datapath.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <net/genetlink.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netns/generic.h>
  51. #include "datapath.h"
  52. #include "flow.h"
  53. #include "flow_table.h"
  54. #include "flow_netlink.h"
  55. #include "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. int ovs_net_id __read_mostly;
  58. static struct genl_family dp_packet_genl_family;
  59. static struct genl_family dp_flow_genl_family;
  60. static struct genl_family dp_datapath_genl_family;
  61. static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  62. .name = OVS_FLOW_MCGROUP,
  63. };
  64. static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  65. .name = OVS_DATAPATH_MCGROUP,
  66. };
  67. static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  68. .name = OVS_VPORT_MCGROUP,
  69. };
  70. /* Check if need to build a reply message.
  71. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  72. static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
  73. unsigned int group)
  74. {
  75. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  76. genl_has_listeners(family, genl_info_net(info)->genl_sock,
  77. group);
  78. }
  79. static void ovs_notify(struct genl_family *family,
  80. struct sk_buff *skb, struct genl_info *info)
  81. {
  82. genl_notify(family, skb, genl_info_net(info), info->snd_portid,
  83. 0, info->nlhdr, GFP_KERNEL);
  84. }
  85. /**
  86. * DOC: Locking:
  87. *
  88. * All writes e.g. Writes to device state (add/remove datapath, port, set
  89. * operations on vports, etc.), Writes to other state (flow table
  90. * modifications, set miscellaneous datapath parameters, etc.) are protected
  91. * by ovs_lock.
  92. *
  93. * Reads are protected by RCU.
  94. *
  95. * There are a few special cases (mostly stats) that have their own
  96. * synchronization but they nest under all of above and don't interact with
  97. * each other.
  98. *
  99. * The RTNL lock nests inside ovs_mutex.
  100. */
  101. static DEFINE_MUTEX(ovs_mutex);
  102. void ovs_lock(void)
  103. {
  104. mutex_lock(&ovs_mutex);
  105. }
  106. void ovs_unlock(void)
  107. {
  108. mutex_unlock(&ovs_mutex);
  109. }
  110. #ifdef CONFIG_LOCKDEP
  111. int lockdep_ovsl_is_held(void)
  112. {
  113. if (debug_locks)
  114. return lockdep_is_held(&ovs_mutex);
  115. else
  116. return 1;
  117. }
  118. #endif
  119. static struct vport *new_vport(const struct vport_parms *);
  120. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  121. const struct dp_upcall_info *);
  122. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  123. const struct dp_upcall_info *);
  124. /* Must be called with rcu_read_lock or ovs_mutex. */
  125. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  126. {
  127. struct datapath *dp = NULL;
  128. struct net_device *dev;
  129. rcu_read_lock();
  130. dev = dev_get_by_index_rcu(net, dp_ifindex);
  131. if (dev) {
  132. struct vport *vport = ovs_internal_dev_get_vport(dev);
  133. if (vport)
  134. dp = vport->dp;
  135. }
  136. rcu_read_unlock();
  137. return dp;
  138. }
  139. /* Must be called with rcu_read_lock or ovs_mutex. */
  140. const char *ovs_dp_name(const struct datapath *dp)
  141. {
  142. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  143. return vport->ops->get_name(vport);
  144. }
  145. static int get_dpifindex(struct datapath *dp)
  146. {
  147. struct vport *local;
  148. int ifindex;
  149. rcu_read_lock();
  150. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  151. if (local)
  152. ifindex = netdev_vport_priv(local)->dev->ifindex;
  153. else
  154. ifindex = 0;
  155. rcu_read_unlock();
  156. return ifindex;
  157. }
  158. static void destroy_dp_rcu(struct rcu_head *rcu)
  159. {
  160. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  161. free_percpu(dp->stats_percpu);
  162. release_net(ovs_dp_get_net(dp));
  163. kfree(dp->ports);
  164. kfree(dp);
  165. }
  166. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  167. u16 port_no)
  168. {
  169. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  170. }
  171. /* Called with ovs_mutex or RCU read lock. */
  172. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  173. {
  174. struct vport *vport;
  175. struct hlist_head *head;
  176. head = vport_hash_bucket(dp, port_no);
  177. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  178. if (vport->port_no == port_no)
  179. return vport;
  180. }
  181. return NULL;
  182. }
  183. /* Called with ovs_mutex. */
  184. static struct vport *new_vport(const struct vport_parms *parms)
  185. {
  186. struct vport *vport;
  187. vport = ovs_vport_add(parms);
  188. if (!IS_ERR(vport)) {
  189. struct datapath *dp = parms->dp;
  190. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  191. hlist_add_head_rcu(&vport->dp_hash_node, head);
  192. }
  193. return vport;
  194. }
  195. void ovs_dp_detach_port(struct vport *p)
  196. {
  197. ASSERT_OVSL();
  198. /* First drop references to device. */
  199. hlist_del_rcu(&p->dp_hash_node);
  200. /* Then destroy it. */
  201. ovs_vport_del(p);
  202. }
  203. /* Must be called with rcu_read_lock. */
  204. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
  205. {
  206. const struct vport *p = OVS_CB(skb)->input_vport;
  207. struct datapath *dp = p->dp;
  208. struct sw_flow *flow;
  209. struct dp_stats_percpu *stats;
  210. u64 *stats_counter;
  211. u32 n_mask_hit;
  212. stats = this_cpu_ptr(dp->stats_percpu);
  213. /* Look up flow. */
  214. flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
  215. if (unlikely(!flow)) {
  216. struct dp_upcall_info upcall;
  217. int error;
  218. upcall.cmd = OVS_PACKET_CMD_MISS;
  219. upcall.key = key;
  220. upcall.userdata = NULL;
  221. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  222. error = ovs_dp_upcall(dp, skb, &upcall);
  223. if (unlikely(error))
  224. kfree_skb(skb);
  225. else
  226. consume_skb(skb);
  227. stats_counter = &stats->n_missed;
  228. goto out;
  229. }
  230. OVS_CB(skb)->flow = flow;
  231. ovs_flow_stats_update(OVS_CB(skb)->flow, key->tp.flags, skb);
  232. ovs_execute_actions(dp, skb, key);
  233. stats_counter = &stats->n_hit;
  234. out:
  235. /* Update datapath statistics. */
  236. u64_stats_update_begin(&stats->syncp);
  237. (*stats_counter)++;
  238. stats->n_mask_hit += n_mask_hit;
  239. u64_stats_update_end(&stats->syncp);
  240. }
  241. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  242. const struct dp_upcall_info *upcall_info)
  243. {
  244. struct dp_stats_percpu *stats;
  245. int err;
  246. if (upcall_info->portid == 0) {
  247. err = -ENOTCONN;
  248. goto err;
  249. }
  250. if (!skb_is_gso(skb))
  251. err = queue_userspace_packet(dp, skb, upcall_info);
  252. else
  253. err = queue_gso_packets(dp, skb, upcall_info);
  254. if (err)
  255. goto err;
  256. return 0;
  257. err:
  258. stats = this_cpu_ptr(dp->stats_percpu);
  259. u64_stats_update_begin(&stats->syncp);
  260. stats->n_lost++;
  261. u64_stats_update_end(&stats->syncp);
  262. return err;
  263. }
  264. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  265. const struct dp_upcall_info *upcall_info)
  266. {
  267. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  268. struct dp_upcall_info later_info;
  269. struct sw_flow_key later_key;
  270. struct sk_buff *segs, *nskb;
  271. int err;
  272. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  273. if (IS_ERR(segs))
  274. return PTR_ERR(segs);
  275. if (segs == NULL)
  276. return -EINVAL;
  277. /* Queue all of the segments. */
  278. skb = segs;
  279. do {
  280. err = queue_userspace_packet(dp, skb, upcall_info);
  281. if (err)
  282. break;
  283. if (skb == segs && gso_type & SKB_GSO_UDP) {
  284. /* The initial flow key extracted by ovs_flow_extract()
  285. * in this case is for a first fragment, so we need to
  286. * properly mark later fragments.
  287. */
  288. later_key = *upcall_info->key;
  289. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  290. later_info = *upcall_info;
  291. later_info.key = &later_key;
  292. upcall_info = &later_info;
  293. }
  294. } while ((skb = skb->next));
  295. /* Free all of the segments. */
  296. skb = segs;
  297. do {
  298. nskb = skb->next;
  299. if (err)
  300. kfree_skb(skb);
  301. else
  302. consume_skb(skb);
  303. } while ((skb = nskb));
  304. return err;
  305. }
  306. static size_t key_attr_size(void)
  307. {
  308. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  309. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  310. + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  311. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  312. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  313. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  314. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  315. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  316. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  317. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
  318. + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
  319. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  320. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  321. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  322. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  323. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  324. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  325. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  326. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  327. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  328. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  329. }
  330. static size_t upcall_msg_size(const struct nlattr *userdata,
  331. unsigned int hdrlen)
  332. {
  333. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  334. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  335. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  336. /* OVS_PACKET_ATTR_USERDATA */
  337. if (userdata)
  338. size += NLA_ALIGN(userdata->nla_len);
  339. return size;
  340. }
  341. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  342. const struct dp_upcall_info *upcall_info)
  343. {
  344. struct ovs_header *upcall;
  345. struct sk_buff *nskb = NULL;
  346. struct sk_buff *user_skb = NULL; /* to be queued to userspace */
  347. struct nlattr *nla;
  348. struct genl_info info = {
  349. .dst_sk = ovs_dp_get_net(dp)->genl_sock,
  350. .snd_portid = upcall_info->portid,
  351. };
  352. size_t len;
  353. unsigned int hlen;
  354. int err, dp_ifindex;
  355. dp_ifindex = get_dpifindex(dp);
  356. if (!dp_ifindex)
  357. return -ENODEV;
  358. if (vlan_tx_tag_present(skb)) {
  359. nskb = skb_clone(skb, GFP_ATOMIC);
  360. if (!nskb)
  361. return -ENOMEM;
  362. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  363. if (!nskb)
  364. return -ENOMEM;
  365. nskb->vlan_tci = 0;
  366. skb = nskb;
  367. }
  368. if (nla_attr_size(skb->len) > USHRT_MAX) {
  369. err = -EFBIG;
  370. goto out;
  371. }
  372. /* Complete checksum if needed */
  373. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  374. (err = skb_checksum_help(skb)))
  375. goto out;
  376. /* Older versions of OVS user space enforce alignment of the last
  377. * Netlink attribute to NLA_ALIGNTO which would require extensive
  378. * padding logic. Only perform zerocopy if padding is not required.
  379. */
  380. if (dp->user_features & OVS_DP_F_UNALIGNED)
  381. hlen = skb_zerocopy_headlen(skb);
  382. else
  383. hlen = skb->len;
  384. len = upcall_msg_size(upcall_info->userdata, hlen);
  385. user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
  386. if (!user_skb) {
  387. err = -ENOMEM;
  388. goto out;
  389. }
  390. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  391. 0, upcall_info->cmd);
  392. upcall->dp_ifindex = dp_ifindex;
  393. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  394. err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
  395. BUG_ON(err);
  396. nla_nest_end(user_skb, nla);
  397. if (upcall_info->userdata)
  398. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  399. nla_len(upcall_info->userdata),
  400. nla_data(upcall_info->userdata));
  401. /* Only reserve room for attribute header, packet data is added
  402. * in skb_zerocopy() */
  403. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  404. err = -ENOBUFS;
  405. goto out;
  406. }
  407. nla->nla_len = nla_attr_size(skb->len);
  408. err = skb_zerocopy(user_skb, skb, skb->len, hlen);
  409. if (err)
  410. goto out;
  411. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  412. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  413. size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
  414. if (plen > 0)
  415. memset(skb_put(user_skb, plen), 0, plen);
  416. }
  417. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  418. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  419. user_skb = NULL;
  420. out:
  421. if (err)
  422. skb_tx_error(skb);
  423. kfree_skb(user_skb);
  424. kfree_skb(nskb);
  425. return err;
  426. }
  427. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  428. {
  429. struct ovs_header *ovs_header = info->userhdr;
  430. struct nlattr **a = info->attrs;
  431. struct sw_flow_actions *acts;
  432. struct sk_buff *packet;
  433. struct sw_flow *flow;
  434. struct datapath *dp;
  435. struct ethhdr *eth;
  436. struct vport *input_vport;
  437. int len;
  438. int err;
  439. err = -EINVAL;
  440. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  441. !a[OVS_PACKET_ATTR_ACTIONS])
  442. goto err;
  443. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  444. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  445. err = -ENOMEM;
  446. if (!packet)
  447. goto err;
  448. skb_reserve(packet, NET_IP_ALIGN);
  449. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  450. skb_reset_mac_header(packet);
  451. eth = eth_hdr(packet);
  452. /* Normally, setting the skb 'protocol' field would be handled by a
  453. * call to eth_type_trans(), but it assumes there's a sending
  454. * device, which we may not have. */
  455. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  456. packet->protocol = eth->h_proto;
  457. else
  458. packet->protocol = htons(ETH_P_802_2);
  459. /* Build an sw_flow for sending this packet. */
  460. flow = ovs_flow_alloc();
  461. err = PTR_ERR(flow);
  462. if (IS_ERR(flow))
  463. goto err_kfree_skb;
  464. err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
  465. &flow->key);
  466. if (err)
  467. goto err_flow_free;
  468. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
  469. err = PTR_ERR(acts);
  470. if (IS_ERR(acts))
  471. goto err_flow_free;
  472. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  473. &flow->key, 0, &acts);
  474. if (err)
  475. goto err_flow_free;
  476. rcu_assign_pointer(flow->sf_acts, acts);
  477. OVS_CB(packet)->egress_tun_info = NULL;
  478. OVS_CB(packet)->flow = flow;
  479. packet->priority = flow->key.phy.priority;
  480. packet->mark = flow->key.phy.skb_mark;
  481. rcu_read_lock();
  482. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  483. err = -ENODEV;
  484. if (!dp)
  485. goto err_unlock;
  486. input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
  487. if (!input_vport)
  488. input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
  489. if (!input_vport)
  490. goto err_unlock;
  491. OVS_CB(packet)->input_vport = input_vport;
  492. local_bh_disable();
  493. err = ovs_execute_actions(dp, packet, &flow->key);
  494. local_bh_enable();
  495. rcu_read_unlock();
  496. ovs_flow_free(flow, false);
  497. return err;
  498. err_unlock:
  499. rcu_read_unlock();
  500. err_flow_free:
  501. ovs_flow_free(flow, false);
  502. err_kfree_skb:
  503. kfree_skb(packet);
  504. err:
  505. return err;
  506. }
  507. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  508. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  509. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  510. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  511. };
  512. static const struct genl_ops dp_packet_genl_ops[] = {
  513. { .cmd = OVS_PACKET_CMD_EXECUTE,
  514. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  515. .policy = packet_policy,
  516. .doit = ovs_packet_cmd_execute
  517. }
  518. };
  519. static struct genl_family dp_packet_genl_family = {
  520. .id = GENL_ID_GENERATE,
  521. .hdrsize = sizeof(struct ovs_header),
  522. .name = OVS_PACKET_FAMILY,
  523. .version = OVS_PACKET_VERSION,
  524. .maxattr = OVS_PACKET_ATTR_MAX,
  525. .netnsok = true,
  526. .parallel_ops = true,
  527. .ops = dp_packet_genl_ops,
  528. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  529. };
  530. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
  531. struct ovs_dp_megaflow_stats *mega_stats)
  532. {
  533. int i;
  534. memset(mega_stats, 0, sizeof(*mega_stats));
  535. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  536. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  537. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  538. for_each_possible_cpu(i) {
  539. const struct dp_stats_percpu *percpu_stats;
  540. struct dp_stats_percpu local_stats;
  541. unsigned int start;
  542. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  543. do {
  544. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  545. local_stats = *percpu_stats;
  546. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  547. stats->n_hit += local_stats.n_hit;
  548. stats->n_missed += local_stats.n_missed;
  549. stats->n_lost += local_stats.n_lost;
  550. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  551. }
  552. }
  553. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  554. {
  555. return NLMSG_ALIGN(sizeof(struct ovs_header))
  556. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  557. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  558. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  559. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  560. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  561. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  562. }
  563. /* Called with ovs_mutex or RCU read lock. */
  564. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  565. struct sk_buff *skb, u32 portid,
  566. u32 seq, u32 flags, u8 cmd)
  567. {
  568. const int skb_orig_len = skb->len;
  569. struct nlattr *start;
  570. struct ovs_flow_stats stats;
  571. __be16 tcp_flags;
  572. unsigned long used;
  573. struct ovs_header *ovs_header;
  574. struct nlattr *nla;
  575. int err;
  576. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  577. if (!ovs_header)
  578. return -EMSGSIZE;
  579. ovs_header->dp_ifindex = dp_ifindex;
  580. /* Fill flow key. */
  581. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  582. if (!nla)
  583. goto nla_put_failure;
  584. err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
  585. if (err)
  586. goto error;
  587. nla_nest_end(skb, nla);
  588. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  589. if (!nla)
  590. goto nla_put_failure;
  591. err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
  592. if (err)
  593. goto error;
  594. nla_nest_end(skb, nla);
  595. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  596. if (used &&
  597. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  598. goto nla_put_failure;
  599. if (stats.n_packets &&
  600. nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
  601. goto nla_put_failure;
  602. if ((u8)ntohs(tcp_flags) &&
  603. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  604. goto nla_put_failure;
  605. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  606. * this is the first flow to be dumped into 'skb'. This is unusual for
  607. * Netlink but individual action lists can be longer than
  608. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  609. * The userspace caller can always fetch the actions separately if it
  610. * really wants them. (Most userspace callers in fact don't care.)
  611. *
  612. * This can only fail for dump operations because the skb is always
  613. * properly sized for single flows.
  614. */
  615. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  616. if (start) {
  617. const struct sw_flow_actions *sf_acts;
  618. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  619. err = ovs_nla_put_actions(sf_acts->actions,
  620. sf_acts->actions_len, skb);
  621. if (!err)
  622. nla_nest_end(skb, start);
  623. else {
  624. if (skb_orig_len)
  625. goto error;
  626. nla_nest_cancel(skb, start);
  627. }
  628. } else if (skb_orig_len)
  629. goto nla_put_failure;
  630. return genlmsg_end(skb, ovs_header);
  631. nla_put_failure:
  632. err = -EMSGSIZE;
  633. error:
  634. genlmsg_cancel(skb, ovs_header);
  635. return err;
  636. }
  637. /* May not be called with RCU read lock. */
  638. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  639. struct genl_info *info,
  640. bool always)
  641. {
  642. struct sk_buff *skb;
  643. if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
  644. return NULL;
  645. skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
  646. if (!skb)
  647. return ERR_PTR(-ENOMEM);
  648. return skb;
  649. }
  650. /* Called with ovs_mutex. */
  651. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  652. int dp_ifindex,
  653. struct genl_info *info, u8 cmd,
  654. bool always)
  655. {
  656. struct sk_buff *skb;
  657. int retval;
  658. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
  659. always);
  660. if (IS_ERR_OR_NULL(skb))
  661. return skb;
  662. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  663. info->snd_portid, info->snd_seq, 0,
  664. cmd);
  665. BUG_ON(retval < 0);
  666. return skb;
  667. }
  668. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  669. {
  670. struct nlattr **a = info->attrs;
  671. struct ovs_header *ovs_header = info->userhdr;
  672. struct sw_flow *flow, *new_flow;
  673. struct sw_flow_mask mask;
  674. struct sk_buff *reply;
  675. struct datapath *dp;
  676. struct sw_flow_actions *acts;
  677. struct sw_flow_match match;
  678. int error;
  679. /* Must have key and actions. */
  680. error = -EINVAL;
  681. if (!a[OVS_FLOW_ATTR_KEY])
  682. goto error;
  683. if (!a[OVS_FLOW_ATTR_ACTIONS])
  684. goto error;
  685. /* Most of the time we need to allocate a new flow, do it before
  686. * locking.
  687. */
  688. new_flow = ovs_flow_alloc();
  689. if (IS_ERR(new_flow)) {
  690. error = PTR_ERR(new_flow);
  691. goto error;
  692. }
  693. /* Extract key. */
  694. ovs_match_init(&match, &new_flow->unmasked_key, &mask);
  695. error = ovs_nla_get_match(&match,
  696. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  697. if (error)
  698. goto err_kfree_flow;
  699. ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
  700. /* Validate actions. */
  701. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  702. error = PTR_ERR(acts);
  703. if (IS_ERR(acts))
  704. goto err_kfree_flow;
  705. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
  706. 0, &acts);
  707. if (error) {
  708. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  709. goto err_kfree_acts;
  710. }
  711. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  712. if (IS_ERR(reply)) {
  713. error = PTR_ERR(reply);
  714. goto err_kfree_acts;
  715. }
  716. ovs_lock();
  717. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  718. if (unlikely(!dp)) {
  719. error = -ENODEV;
  720. goto err_unlock_ovs;
  721. }
  722. /* Check if this is a duplicate flow */
  723. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
  724. if (likely(!flow)) {
  725. rcu_assign_pointer(new_flow->sf_acts, acts);
  726. /* Put flow in bucket. */
  727. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  728. if (unlikely(error)) {
  729. acts = NULL;
  730. goto err_unlock_ovs;
  731. }
  732. if (unlikely(reply)) {
  733. error = ovs_flow_cmd_fill_info(new_flow,
  734. ovs_header->dp_ifindex,
  735. reply, info->snd_portid,
  736. info->snd_seq, 0,
  737. OVS_FLOW_CMD_NEW);
  738. BUG_ON(error < 0);
  739. }
  740. ovs_unlock();
  741. } else {
  742. struct sw_flow_actions *old_acts;
  743. /* Bail out if we're not allowed to modify an existing flow.
  744. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  745. * because Generic Netlink treats the latter as a dump
  746. * request. We also accept NLM_F_EXCL in case that bug ever
  747. * gets fixed.
  748. */
  749. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  750. | NLM_F_EXCL))) {
  751. error = -EEXIST;
  752. goto err_unlock_ovs;
  753. }
  754. /* The unmasked key has to be the same for flow updates. */
  755. if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
  756. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  757. if (!flow) {
  758. error = -ENOENT;
  759. goto err_unlock_ovs;
  760. }
  761. }
  762. /* Update actions. */
  763. old_acts = ovsl_dereference(flow->sf_acts);
  764. rcu_assign_pointer(flow->sf_acts, acts);
  765. if (unlikely(reply)) {
  766. error = ovs_flow_cmd_fill_info(flow,
  767. ovs_header->dp_ifindex,
  768. reply, info->snd_portid,
  769. info->snd_seq, 0,
  770. OVS_FLOW_CMD_NEW);
  771. BUG_ON(error < 0);
  772. }
  773. ovs_unlock();
  774. ovs_nla_free_flow_actions(old_acts);
  775. ovs_flow_free(new_flow, false);
  776. }
  777. if (reply)
  778. ovs_notify(&dp_flow_genl_family, reply, info);
  779. return 0;
  780. err_unlock_ovs:
  781. ovs_unlock();
  782. kfree_skb(reply);
  783. err_kfree_acts:
  784. kfree(acts);
  785. err_kfree_flow:
  786. ovs_flow_free(new_flow, false);
  787. error:
  788. return error;
  789. }
  790. static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
  791. const struct sw_flow_key *key,
  792. const struct sw_flow_mask *mask)
  793. {
  794. struct sw_flow_actions *acts;
  795. struct sw_flow_key masked_key;
  796. int error;
  797. acts = ovs_nla_alloc_flow_actions(nla_len(a));
  798. if (IS_ERR(acts))
  799. return acts;
  800. ovs_flow_mask_key(&masked_key, key, mask);
  801. error = ovs_nla_copy_actions(a, &masked_key, 0, &acts);
  802. if (error) {
  803. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  804. kfree(acts);
  805. return ERR_PTR(error);
  806. }
  807. return acts;
  808. }
  809. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  810. {
  811. struct nlattr **a = info->attrs;
  812. struct ovs_header *ovs_header = info->userhdr;
  813. struct sw_flow_key key;
  814. struct sw_flow *flow;
  815. struct sw_flow_mask mask;
  816. struct sk_buff *reply = NULL;
  817. struct datapath *dp;
  818. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  819. struct sw_flow_match match;
  820. int error;
  821. /* Extract key. */
  822. error = -EINVAL;
  823. if (!a[OVS_FLOW_ATTR_KEY])
  824. goto error;
  825. ovs_match_init(&match, &key, &mask);
  826. error = ovs_nla_get_match(&match,
  827. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  828. if (error)
  829. goto error;
  830. /* Validate actions. */
  831. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  832. acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
  833. if (IS_ERR(acts)) {
  834. error = PTR_ERR(acts);
  835. goto error;
  836. }
  837. }
  838. /* Can allocate before locking if have acts. */
  839. if (acts) {
  840. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  841. if (IS_ERR(reply)) {
  842. error = PTR_ERR(reply);
  843. goto err_kfree_acts;
  844. }
  845. }
  846. ovs_lock();
  847. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  848. if (unlikely(!dp)) {
  849. error = -ENODEV;
  850. goto err_unlock_ovs;
  851. }
  852. /* Check that the flow exists. */
  853. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  854. if (unlikely(!flow)) {
  855. error = -ENOENT;
  856. goto err_unlock_ovs;
  857. }
  858. /* Update actions, if present. */
  859. if (likely(acts)) {
  860. old_acts = ovsl_dereference(flow->sf_acts);
  861. rcu_assign_pointer(flow->sf_acts, acts);
  862. if (unlikely(reply)) {
  863. error = ovs_flow_cmd_fill_info(flow,
  864. ovs_header->dp_ifindex,
  865. reply, info->snd_portid,
  866. info->snd_seq, 0,
  867. OVS_FLOW_CMD_NEW);
  868. BUG_ON(error < 0);
  869. }
  870. } else {
  871. /* Could not alloc without acts before locking. */
  872. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  873. info, OVS_FLOW_CMD_NEW, false);
  874. if (unlikely(IS_ERR(reply))) {
  875. error = PTR_ERR(reply);
  876. goto err_unlock_ovs;
  877. }
  878. }
  879. /* Clear stats. */
  880. if (a[OVS_FLOW_ATTR_CLEAR])
  881. ovs_flow_stats_clear(flow);
  882. ovs_unlock();
  883. if (reply)
  884. ovs_notify(&dp_flow_genl_family, reply, info);
  885. if (old_acts)
  886. ovs_nla_free_flow_actions(old_acts);
  887. return 0;
  888. err_unlock_ovs:
  889. ovs_unlock();
  890. kfree_skb(reply);
  891. err_kfree_acts:
  892. kfree(acts);
  893. error:
  894. return error;
  895. }
  896. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  897. {
  898. struct nlattr **a = info->attrs;
  899. struct ovs_header *ovs_header = info->userhdr;
  900. struct sw_flow_key key;
  901. struct sk_buff *reply;
  902. struct sw_flow *flow;
  903. struct datapath *dp;
  904. struct sw_flow_match match;
  905. int err;
  906. if (!a[OVS_FLOW_ATTR_KEY]) {
  907. OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
  908. return -EINVAL;
  909. }
  910. ovs_match_init(&match, &key, NULL);
  911. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  912. if (err)
  913. return err;
  914. ovs_lock();
  915. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  916. if (!dp) {
  917. err = -ENODEV;
  918. goto unlock;
  919. }
  920. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  921. if (!flow) {
  922. err = -ENOENT;
  923. goto unlock;
  924. }
  925. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  926. OVS_FLOW_CMD_NEW, true);
  927. if (IS_ERR(reply)) {
  928. err = PTR_ERR(reply);
  929. goto unlock;
  930. }
  931. ovs_unlock();
  932. return genlmsg_reply(reply, info);
  933. unlock:
  934. ovs_unlock();
  935. return err;
  936. }
  937. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  938. {
  939. struct nlattr **a = info->attrs;
  940. struct ovs_header *ovs_header = info->userhdr;
  941. struct sw_flow_key key;
  942. struct sk_buff *reply;
  943. struct sw_flow *flow;
  944. struct datapath *dp;
  945. struct sw_flow_match match;
  946. int err;
  947. if (likely(a[OVS_FLOW_ATTR_KEY])) {
  948. ovs_match_init(&match, &key, NULL);
  949. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  950. if (unlikely(err))
  951. return err;
  952. }
  953. ovs_lock();
  954. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  955. if (unlikely(!dp)) {
  956. err = -ENODEV;
  957. goto unlock;
  958. }
  959. if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
  960. err = ovs_flow_tbl_flush(&dp->table);
  961. goto unlock;
  962. }
  963. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  964. if (unlikely(!flow)) {
  965. err = -ENOENT;
  966. goto unlock;
  967. }
  968. ovs_flow_tbl_remove(&dp->table, flow);
  969. ovs_unlock();
  970. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  971. info, false);
  972. if (likely(reply)) {
  973. if (likely(!IS_ERR(reply))) {
  974. rcu_read_lock(); /*To keep RCU checker happy. */
  975. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  976. reply, info->snd_portid,
  977. info->snd_seq, 0,
  978. OVS_FLOW_CMD_DEL);
  979. rcu_read_unlock();
  980. BUG_ON(err < 0);
  981. ovs_notify(&dp_flow_genl_family, reply, info);
  982. } else {
  983. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  984. }
  985. }
  986. ovs_flow_free(flow, true);
  987. return 0;
  988. unlock:
  989. ovs_unlock();
  990. return err;
  991. }
  992. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  993. {
  994. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  995. struct table_instance *ti;
  996. struct datapath *dp;
  997. rcu_read_lock();
  998. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  999. if (!dp) {
  1000. rcu_read_unlock();
  1001. return -ENODEV;
  1002. }
  1003. ti = rcu_dereference(dp->table.ti);
  1004. for (;;) {
  1005. struct sw_flow *flow;
  1006. u32 bucket, obj;
  1007. bucket = cb->args[0];
  1008. obj = cb->args[1];
  1009. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  1010. if (!flow)
  1011. break;
  1012. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  1013. NETLINK_CB(cb->skb).portid,
  1014. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1015. OVS_FLOW_CMD_NEW) < 0)
  1016. break;
  1017. cb->args[0] = bucket;
  1018. cb->args[1] = obj;
  1019. }
  1020. rcu_read_unlock();
  1021. return skb->len;
  1022. }
  1023. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1024. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1025. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1026. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1027. };
  1028. static const struct genl_ops dp_flow_genl_ops[] = {
  1029. { .cmd = OVS_FLOW_CMD_NEW,
  1030. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1031. .policy = flow_policy,
  1032. .doit = ovs_flow_cmd_new
  1033. },
  1034. { .cmd = OVS_FLOW_CMD_DEL,
  1035. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1036. .policy = flow_policy,
  1037. .doit = ovs_flow_cmd_del
  1038. },
  1039. { .cmd = OVS_FLOW_CMD_GET,
  1040. .flags = 0, /* OK for unprivileged users. */
  1041. .policy = flow_policy,
  1042. .doit = ovs_flow_cmd_get,
  1043. .dumpit = ovs_flow_cmd_dump
  1044. },
  1045. { .cmd = OVS_FLOW_CMD_SET,
  1046. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1047. .policy = flow_policy,
  1048. .doit = ovs_flow_cmd_set,
  1049. },
  1050. };
  1051. static struct genl_family dp_flow_genl_family = {
  1052. .id = GENL_ID_GENERATE,
  1053. .hdrsize = sizeof(struct ovs_header),
  1054. .name = OVS_FLOW_FAMILY,
  1055. .version = OVS_FLOW_VERSION,
  1056. .maxattr = OVS_FLOW_ATTR_MAX,
  1057. .netnsok = true,
  1058. .parallel_ops = true,
  1059. .ops = dp_flow_genl_ops,
  1060. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1061. .mcgrps = &ovs_dp_flow_multicast_group,
  1062. .n_mcgrps = 1,
  1063. };
  1064. static size_t ovs_dp_cmd_msg_size(void)
  1065. {
  1066. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1067. msgsize += nla_total_size(IFNAMSIZ);
  1068. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1069. msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
  1070. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1071. return msgsize;
  1072. }
  1073. /* Called with ovs_mutex or RCU read lock. */
  1074. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1075. u32 portid, u32 seq, u32 flags, u8 cmd)
  1076. {
  1077. struct ovs_header *ovs_header;
  1078. struct ovs_dp_stats dp_stats;
  1079. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1080. int err;
  1081. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1082. flags, cmd);
  1083. if (!ovs_header)
  1084. goto error;
  1085. ovs_header->dp_ifindex = get_dpifindex(dp);
  1086. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1087. if (err)
  1088. goto nla_put_failure;
  1089. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1090. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1091. &dp_stats))
  1092. goto nla_put_failure;
  1093. if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1094. sizeof(struct ovs_dp_megaflow_stats),
  1095. &dp_megaflow_stats))
  1096. goto nla_put_failure;
  1097. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1098. goto nla_put_failure;
  1099. return genlmsg_end(skb, ovs_header);
  1100. nla_put_failure:
  1101. genlmsg_cancel(skb, ovs_header);
  1102. error:
  1103. return -EMSGSIZE;
  1104. }
  1105. static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
  1106. {
  1107. return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
  1108. }
  1109. /* Called with rcu_read_lock or ovs_mutex. */
  1110. static struct datapath *lookup_datapath(struct net *net,
  1111. struct ovs_header *ovs_header,
  1112. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1113. {
  1114. struct datapath *dp;
  1115. if (!a[OVS_DP_ATTR_NAME])
  1116. dp = get_dp(net, ovs_header->dp_ifindex);
  1117. else {
  1118. struct vport *vport;
  1119. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1120. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1121. }
  1122. return dp ? dp : ERR_PTR(-ENODEV);
  1123. }
  1124. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1125. {
  1126. struct datapath *dp;
  1127. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1128. if (IS_ERR(dp))
  1129. return;
  1130. WARN(dp->user_features, "Dropping previously announced user features\n");
  1131. dp->user_features = 0;
  1132. }
  1133. static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
  1134. {
  1135. if (a[OVS_DP_ATTR_USER_FEATURES])
  1136. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1137. }
  1138. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1139. {
  1140. struct nlattr **a = info->attrs;
  1141. struct vport_parms parms;
  1142. struct sk_buff *reply;
  1143. struct datapath *dp;
  1144. struct vport *vport;
  1145. struct ovs_net *ovs_net;
  1146. int err, i;
  1147. err = -EINVAL;
  1148. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1149. goto err;
  1150. reply = ovs_dp_cmd_alloc_info(info);
  1151. if (!reply)
  1152. return -ENOMEM;
  1153. err = -ENOMEM;
  1154. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1155. if (dp == NULL)
  1156. goto err_free_reply;
  1157. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1158. /* Allocate table. */
  1159. err = ovs_flow_tbl_init(&dp->table);
  1160. if (err)
  1161. goto err_free_dp;
  1162. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1163. if (!dp->stats_percpu) {
  1164. err = -ENOMEM;
  1165. goto err_destroy_table;
  1166. }
  1167. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1168. GFP_KERNEL);
  1169. if (!dp->ports) {
  1170. err = -ENOMEM;
  1171. goto err_destroy_percpu;
  1172. }
  1173. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1174. INIT_HLIST_HEAD(&dp->ports[i]);
  1175. /* Set up our datapath device. */
  1176. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1177. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1178. parms.options = NULL;
  1179. parms.dp = dp;
  1180. parms.port_no = OVSP_LOCAL;
  1181. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1182. ovs_dp_change(dp, a);
  1183. /* So far only local changes have been made, now need the lock. */
  1184. ovs_lock();
  1185. vport = new_vport(&parms);
  1186. if (IS_ERR(vport)) {
  1187. err = PTR_ERR(vport);
  1188. if (err == -EBUSY)
  1189. err = -EEXIST;
  1190. if (err == -EEXIST) {
  1191. /* An outdated user space instance that does not understand
  1192. * the concept of user_features has attempted to create a new
  1193. * datapath and is likely to reuse it. Drop all user features.
  1194. */
  1195. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1196. ovs_dp_reset_user_features(skb, info);
  1197. }
  1198. goto err_destroy_ports_array;
  1199. }
  1200. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1201. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1202. BUG_ON(err < 0);
  1203. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1204. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1205. ovs_unlock();
  1206. ovs_notify(&dp_datapath_genl_family, reply, info);
  1207. return 0;
  1208. err_destroy_ports_array:
  1209. ovs_unlock();
  1210. kfree(dp->ports);
  1211. err_destroy_percpu:
  1212. free_percpu(dp->stats_percpu);
  1213. err_destroy_table:
  1214. ovs_flow_tbl_destroy(&dp->table, false);
  1215. err_free_dp:
  1216. release_net(ovs_dp_get_net(dp));
  1217. kfree(dp);
  1218. err_free_reply:
  1219. kfree_skb(reply);
  1220. err:
  1221. return err;
  1222. }
  1223. /* Called with ovs_mutex. */
  1224. static void __dp_destroy(struct datapath *dp)
  1225. {
  1226. int i;
  1227. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1228. struct vport *vport;
  1229. struct hlist_node *n;
  1230. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1231. if (vport->port_no != OVSP_LOCAL)
  1232. ovs_dp_detach_port(vport);
  1233. }
  1234. list_del_rcu(&dp->list_node);
  1235. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1236. * all ports in datapath are destroyed first before freeing datapath.
  1237. */
  1238. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1239. /* RCU destroy the flow table */
  1240. ovs_flow_tbl_destroy(&dp->table, true);
  1241. call_rcu(&dp->rcu, destroy_dp_rcu);
  1242. }
  1243. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1244. {
  1245. struct sk_buff *reply;
  1246. struct datapath *dp;
  1247. int err;
  1248. reply = ovs_dp_cmd_alloc_info(info);
  1249. if (!reply)
  1250. return -ENOMEM;
  1251. ovs_lock();
  1252. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1253. err = PTR_ERR(dp);
  1254. if (IS_ERR(dp))
  1255. goto err_unlock_free;
  1256. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1257. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1258. BUG_ON(err < 0);
  1259. __dp_destroy(dp);
  1260. ovs_unlock();
  1261. ovs_notify(&dp_datapath_genl_family, reply, info);
  1262. return 0;
  1263. err_unlock_free:
  1264. ovs_unlock();
  1265. kfree_skb(reply);
  1266. return err;
  1267. }
  1268. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1269. {
  1270. struct sk_buff *reply;
  1271. struct datapath *dp;
  1272. int err;
  1273. reply = ovs_dp_cmd_alloc_info(info);
  1274. if (!reply)
  1275. return -ENOMEM;
  1276. ovs_lock();
  1277. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1278. err = PTR_ERR(dp);
  1279. if (IS_ERR(dp))
  1280. goto err_unlock_free;
  1281. ovs_dp_change(dp, info->attrs);
  1282. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1283. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1284. BUG_ON(err < 0);
  1285. ovs_unlock();
  1286. ovs_notify(&dp_datapath_genl_family, reply, info);
  1287. return 0;
  1288. err_unlock_free:
  1289. ovs_unlock();
  1290. kfree_skb(reply);
  1291. return err;
  1292. }
  1293. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1294. {
  1295. struct sk_buff *reply;
  1296. struct datapath *dp;
  1297. int err;
  1298. reply = ovs_dp_cmd_alloc_info(info);
  1299. if (!reply)
  1300. return -ENOMEM;
  1301. rcu_read_lock();
  1302. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1303. if (IS_ERR(dp)) {
  1304. err = PTR_ERR(dp);
  1305. goto err_unlock_free;
  1306. }
  1307. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1308. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1309. BUG_ON(err < 0);
  1310. rcu_read_unlock();
  1311. return genlmsg_reply(reply, info);
  1312. err_unlock_free:
  1313. rcu_read_unlock();
  1314. kfree_skb(reply);
  1315. return err;
  1316. }
  1317. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1318. {
  1319. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1320. struct datapath *dp;
  1321. int skip = cb->args[0];
  1322. int i = 0;
  1323. rcu_read_lock();
  1324. list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
  1325. if (i >= skip &&
  1326. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1327. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1328. OVS_DP_CMD_NEW) < 0)
  1329. break;
  1330. i++;
  1331. }
  1332. rcu_read_unlock();
  1333. cb->args[0] = i;
  1334. return skb->len;
  1335. }
  1336. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1337. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1338. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1339. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1340. };
  1341. static const struct genl_ops dp_datapath_genl_ops[] = {
  1342. { .cmd = OVS_DP_CMD_NEW,
  1343. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1344. .policy = datapath_policy,
  1345. .doit = ovs_dp_cmd_new
  1346. },
  1347. { .cmd = OVS_DP_CMD_DEL,
  1348. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1349. .policy = datapath_policy,
  1350. .doit = ovs_dp_cmd_del
  1351. },
  1352. { .cmd = OVS_DP_CMD_GET,
  1353. .flags = 0, /* OK for unprivileged users. */
  1354. .policy = datapath_policy,
  1355. .doit = ovs_dp_cmd_get,
  1356. .dumpit = ovs_dp_cmd_dump
  1357. },
  1358. { .cmd = OVS_DP_CMD_SET,
  1359. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1360. .policy = datapath_policy,
  1361. .doit = ovs_dp_cmd_set,
  1362. },
  1363. };
  1364. static struct genl_family dp_datapath_genl_family = {
  1365. .id = GENL_ID_GENERATE,
  1366. .hdrsize = sizeof(struct ovs_header),
  1367. .name = OVS_DATAPATH_FAMILY,
  1368. .version = OVS_DATAPATH_VERSION,
  1369. .maxattr = OVS_DP_ATTR_MAX,
  1370. .netnsok = true,
  1371. .parallel_ops = true,
  1372. .ops = dp_datapath_genl_ops,
  1373. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1374. .mcgrps = &ovs_dp_datapath_multicast_group,
  1375. .n_mcgrps = 1,
  1376. };
  1377. /* Called with ovs_mutex or RCU read lock. */
  1378. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1379. u32 portid, u32 seq, u32 flags, u8 cmd)
  1380. {
  1381. struct ovs_header *ovs_header;
  1382. struct ovs_vport_stats vport_stats;
  1383. int err;
  1384. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1385. flags, cmd);
  1386. if (!ovs_header)
  1387. return -EMSGSIZE;
  1388. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1389. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1390. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1391. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1392. vport->ops->get_name(vport)))
  1393. goto nla_put_failure;
  1394. ovs_vport_get_stats(vport, &vport_stats);
  1395. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1396. &vport_stats))
  1397. goto nla_put_failure;
  1398. if (ovs_vport_get_upcall_portids(vport, skb))
  1399. goto nla_put_failure;
  1400. err = ovs_vport_get_options(vport, skb);
  1401. if (err == -EMSGSIZE)
  1402. goto error;
  1403. return genlmsg_end(skb, ovs_header);
  1404. nla_put_failure:
  1405. err = -EMSGSIZE;
  1406. error:
  1407. genlmsg_cancel(skb, ovs_header);
  1408. return err;
  1409. }
  1410. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1411. {
  1412. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1413. }
  1414. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1415. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1416. u32 seq, u8 cmd)
  1417. {
  1418. struct sk_buff *skb;
  1419. int retval;
  1420. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1421. if (!skb)
  1422. return ERR_PTR(-ENOMEM);
  1423. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1424. BUG_ON(retval < 0);
  1425. return skb;
  1426. }
  1427. /* Called with ovs_mutex or RCU read lock. */
  1428. static struct vport *lookup_vport(struct net *net,
  1429. struct ovs_header *ovs_header,
  1430. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1431. {
  1432. struct datapath *dp;
  1433. struct vport *vport;
  1434. if (a[OVS_VPORT_ATTR_NAME]) {
  1435. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1436. if (!vport)
  1437. return ERR_PTR(-ENODEV);
  1438. if (ovs_header->dp_ifindex &&
  1439. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1440. return ERR_PTR(-ENODEV);
  1441. return vport;
  1442. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1443. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1444. if (port_no >= DP_MAX_PORTS)
  1445. return ERR_PTR(-EFBIG);
  1446. dp = get_dp(net, ovs_header->dp_ifindex);
  1447. if (!dp)
  1448. return ERR_PTR(-ENODEV);
  1449. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1450. if (!vport)
  1451. return ERR_PTR(-ENODEV);
  1452. return vport;
  1453. } else
  1454. return ERR_PTR(-EINVAL);
  1455. }
  1456. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1457. {
  1458. struct nlattr **a = info->attrs;
  1459. struct ovs_header *ovs_header = info->userhdr;
  1460. struct vport_parms parms;
  1461. struct sk_buff *reply;
  1462. struct vport *vport;
  1463. struct datapath *dp;
  1464. u32 port_no;
  1465. int err;
  1466. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1467. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1468. return -EINVAL;
  1469. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1470. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1471. if (port_no >= DP_MAX_PORTS)
  1472. return -EFBIG;
  1473. reply = ovs_vport_cmd_alloc_info();
  1474. if (!reply)
  1475. return -ENOMEM;
  1476. ovs_lock();
  1477. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1478. err = -ENODEV;
  1479. if (!dp)
  1480. goto exit_unlock_free;
  1481. if (port_no) {
  1482. vport = ovs_vport_ovsl(dp, port_no);
  1483. err = -EBUSY;
  1484. if (vport)
  1485. goto exit_unlock_free;
  1486. } else {
  1487. for (port_no = 1; ; port_no++) {
  1488. if (port_no >= DP_MAX_PORTS) {
  1489. err = -EFBIG;
  1490. goto exit_unlock_free;
  1491. }
  1492. vport = ovs_vport_ovsl(dp, port_no);
  1493. if (!vport)
  1494. break;
  1495. }
  1496. }
  1497. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1498. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1499. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1500. parms.dp = dp;
  1501. parms.port_no = port_no;
  1502. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1503. vport = new_vport(&parms);
  1504. err = PTR_ERR(vport);
  1505. if (IS_ERR(vport))
  1506. goto exit_unlock_free;
  1507. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1508. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1509. BUG_ON(err < 0);
  1510. ovs_unlock();
  1511. ovs_notify(&dp_vport_genl_family, reply, info);
  1512. return 0;
  1513. exit_unlock_free:
  1514. ovs_unlock();
  1515. kfree_skb(reply);
  1516. return err;
  1517. }
  1518. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1519. {
  1520. struct nlattr **a = info->attrs;
  1521. struct sk_buff *reply;
  1522. struct vport *vport;
  1523. int err;
  1524. reply = ovs_vport_cmd_alloc_info();
  1525. if (!reply)
  1526. return -ENOMEM;
  1527. ovs_lock();
  1528. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1529. err = PTR_ERR(vport);
  1530. if (IS_ERR(vport))
  1531. goto exit_unlock_free;
  1532. if (a[OVS_VPORT_ATTR_TYPE] &&
  1533. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1534. err = -EINVAL;
  1535. goto exit_unlock_free;
  1536. }
  1537. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1538. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1539. if (err)
  1540. goto exit_unlock_free;
  1541. }
  1542. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1543. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1544. err = ovs_vport_set_upcall_portids(vport, ids);
  1545. if (err)
  1546. goto exit_unlock_free;
  1547. }
  1548. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1549. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1550. BUG_ON(err < 0);
  1551. ovs_unlock();
  1552. ovs_notify(&dp_vport_genl_family, reply, info);
  1553. return 0;
  1554. exit_unlock_free:
  1555. ovs_unlock();
  1556. kfree_skb(reply);
  1557. return err;
  1558. }
  1559. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1560. {
  1561. struct nlattr **a = info->attrs;
  1562. struct sk_buff *reply;
  1563. struct vport *vport;
  1564. int err;
  1565. reply = ovs_vport_cmd_alloc_info();
  1566. if (!reply)
  1567. return -ENOMEM;
  1568. ovs_lock();
  1569. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1570. err = PTR_ERR(vport);
  1571. if (IS_ERR(vport))
  1572. goto exit_unlock_free;
  1573. if (vport->port_no == OVSP_LOCAL) {
  1574. err = -EINVAL;
  1575. goto exit_unlock_free;
  1576. }
  1577. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1578. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1579. BUG_ON(err < 0);
  1580. ovs_dp_detach_port(vport);
  1581. ovs_unlock();
  1582. ovs_notify(&dp_vport_genl_family, reply, info);
  1583. return 0;
  1584. exit_unlock_free:
  1585. ovs_unlock();
  1586. kfree_skb(reply);
  1587. return err;
  1588. }
  1589. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1590. {
  1591. struct nlattr **a = info->attrs;
  1592. struct ovs_header *ovs_header = info->userhdr;
  1593. struct sk_buff *reply;
  1594. struct vport *vport;
  1595. int err;
  1596. reply = ovs_vport_cmd_alloc_info();
  1597. if (!reply)
  1598. return -ENOMEM;
  1599. rcu_read_lock();
  1600. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1601. err = PTR_ERR(vport);
  1602. if (IS_ERR(vport))
  1603. goto exit_unlock_free;
  1604. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1605. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1606. BUG_ON(err < 0);
  1607. rcu_read_unlock();
  1608. return genlmsg_reply(reply, info);
  1609. exit_unlock_free:
  1610. rcu_read_unlock();
  1611. kfree_skb(reply);
  1612. return err;
  1613. }
  1614. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1615. {
  1616. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1617. struct datapath *dp;
  1618. int bucket = cb->args[0], skip = cb->args[1];
  1619. int i, j = 0;
  1620. rcu_read_lock();
  1621. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1622. if (!dp) {
  1623. rcu_read_unlock();
  1624. return -ENODEV;
  1625. }
  1626. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1627. struct vport *vport;
  1628. j = 0;
  1629. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1630. if (j >= skip &&
  1631. ovs_vport_cmd_fill_info(vport, skb,
  1632. NETLINK_CB(cb->skb).portid,
  1633. cb->nlh->nlmsg_seq,
  1634. NLM_F_MULTI,
  1635. OVS_VPORT_CMD_NEW) < 0)
  1636. goto out;
  1637. j++;
  1638. }
  1639. skip = 0;
  1640. }
  1641. out:
  1642. rcu_read_unlock();
  1643. cb->args[0] = i;
  1644. cb->args[1] = j;
  1645. return skb->len;
  1646. }
  1647. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1648. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1649. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1650. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1651. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1652. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1653. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1654. };
  1655. static const struct genl_ops dp_vport_genl_ops[] = {
  1656. { .cmd = OVS_VPORT_CMD_NEW,
  1657. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1658. .policy = vport_policy,
  1659. .doit = ovs_vport_cmd_new
  1660. },
  1661. { .cmd = OVS_VPORT_CMD_DEL,
  1662. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1663. .policy = vport_policy,
  1664. .doit = ovs_vport_cmd_del
  1665. },
  1666. { .cmd = OVS_VPORT_CMD_GET,
  1667. .flags = 0, /* OK for unprivileged users. */
  1668. .policy = vport_policy,
  1669. .doit = ovs_vport_cmd_get,
  1670. .dumpit = ovs_vport_cmd_dump
  1671. },
  1672. { .cmd = OVS_VPORT_CMD_SET,
  1673. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1674. .policy = vport_policy,
  1675. .doit = ovs_vport_cmd_set,
  1676. },
  1677. };
  1678. struct genl_family dp_vport_genl_family = {
  1679. .id = GENL_ID_GENERATE,
  1680. .hdrsize = sizeof(struct ovs_header),
  1681. .name = OVS_VPORT_FAMILY,
  1682. .version = OVS_VPORT_VERSION,
  1683. .maxattr = OVS_VPORT_ATTR_MAX,
  1684. .netnsok = true,
  1685. .parallel_ops = true,
  1686. .ops = dp_vport_genl_ops,
  1687. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1688. .mcgrps = &ovs_dp_vport_multicast_group,
  1689. .n_mcgrps = 1,
  1690. };
  1691. static struct genl_family * const dp_genl_families[] = {
  1692. &dp_datapath_genl_family,
  1693. &dp_vport_genl_family,
  1694. &dp_flow_genl_family,
  1695. &dp_packet_genl_family,
  1696. };
  1697. static void dp_unregister_genl(int n_families)
  1698. {
  1699. int i;
  1700. for (i = 0; i < n_families; i++)
  1701. genl_unregister_family(dp_genl_families[i]);
  1702. }
  1703. static int dp_register_genl(void)
  1704. {
  1705. int err;
  1706. int i;
  1707. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1708. err = genl_register_family(dp_genl_families[i]);
  1709. if (err)
  1710. goto error;
  1711. }
  1712. return 0;
  1713. error:
  1714. dp_unregister_genl(i);
  1715. return err;
  1716. }
  1717. static int __net_init ovs_init_net(struct net *net)
  1718. {
  1719. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1720. INIT_LIST_HEAD(&ovs_net->dps);
  1721. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1722. return 0;
  1723. }
  1724. static void __net_exit ovs_exit_net(struct net *net)
  1725. {
  1726. struct datapath *dp, *dp_next;
  1727. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1728. ovs_lock();
  1729. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1730. __dp_destroy(dp);
  1731. ovs_unlock();
  1732. cancel_work_sync(&ovs_net->dp_notify_work);
  1733. }
  1734. static struct pernet_operations ovs_net_ops = {
  1735. .init = ovs_init_net,
  1736. .exit = ovs_exit_net,
  1737. .id = &ovs_net_id,
  1738. .size = sizeof(struct ovs_net),
  1739. };
  1740. static int __init dp_init(void)
  1741. {
  1742. int err;
  1743. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1744. pr_info("Open vSwitch switching datapath\n");
  1745. err = action_fifos_init();
  1746. if (err)
  1747. goto error;
  1748. err = ovs_internal_dev_rtnl_link_register();
  1749. if (err)
  1750. goto error_action_fifos_exit;
  1751. err = ovs_flow_init();
  1752. if (err)
  1753. goto error_unreg_rtnl_link;
  1754. err = ovs_vport_init();
  1755. if (err)
  1756. goto error_flow_exit;
  1757. err = register_pernet_device(&ovs_net_ops);
  1758. if (err)
  1759. goto error_vport_exit;
  1760. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1761. if (err)
  1762. goto error_netns_exit;
  1763. err = dp_register_genl();
  1764. if (err < 0)
  1765. goto error_unreg_notifier;
  1766. return 0;
  1767. error_unreg_notifier:
  1768. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1769. error_netns_exit:
  1770. unregister_pernet_device(&ovs_net_ops);
  1771. error_vport_exit:
  1772. ovs_vport_exit();
  1773. error_flow_exit:
  1774. ovs_flow_exit();
  1775. error_unreg_rtnl_link:
  1776. ovs_internal_dev_rtnl_link_unregister();
  1777. error_action_fifos_exit:
  1778. action_fifos_exit();
  1779. error:
  1780. return err;
  1781. }
  1782. static void dp_cleanup(void)
  1783. {
  1784. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1785. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1786. unregister_pernet_device(&ovs_net_ops);
  1787. rcu_barrier();
  1788. ovs_vport_exit();
  1789. ovs_flow_exit();
  1790. ovs_internal_dev_rtnl_link_unregister();
  1791. action_fifos_exit();
  1792. }
  1793. module_init(dp_init);
  1794. module_exit(dp_cleanup);
  1795. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1796. MODULE_LICENSE("GPL");