datapath.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050
  1. /*
  2. * Copyright (c) 2007-2012 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 <linux/workqueue.h>
  49. #include <net/genetlink.h>
  50. #include <net/net_namespace.h>
  51. #include <net/netns/generic.h>
  52. #include "datapath.h"
  53. #include "flow.h"
  54. #include "vport-internal_dev.h"
  55. /**
  56. * struct ovs_net - Per net-namespace data for ovs.
  57. * @dps: List of datapaths to enable dumping them all out.
  58. * Protected by genl_mutex.
  59. */
  60. struct ovs_net {
  61. struct list_head dps;
  62. };
  63. static int ovs_net_id __read_mostly;
  64. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  65. static void rehash_flow_table(struct work_struct *work);
  66. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  67. /**
  68. * DOC: Locking:
  69. *
  70. * Writes to device state (add/remove datapath, port, set operations on vports,
  71. * etc.) are protected by RTNL.
  72. *
  73. * Writes to other state (flow table modifications, set miscellaneous datapath
  74. * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
  75. * genl_mutex.
  76. *
  77. * Reads are protected by RCU.
  78. *
  79. * There are a few special cases (mostly stats) that have their own
  80. * synchronization but they nest under all of above and don't interact with
  81. * each other.
  82. */
  83. static struct vport *new_vport(const struct vport_parms *);
  84. static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
  85. const struct dp_upcall_info *);
  86. static int queue_userspace_packet(struct net *, int dp_ifindex,
  87. struct sk_buff *,
  88. const struct dp_upcall_info *);
  89. /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
  90. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  91. {
  92. struct datapath *dp = NULL;
  93. struct net_device *dev;
  94. rcu_read_lock();
  95. dev = dev_get_by_index_rcu(net, dp_ifindex);
  96. if (dev) {
  97. struct vport *vport = ovs_internal_dev_get_vport(dev);
  98. if (vport)
  99. dp = vport->dp;
  100. }
  101. rcu_read_unlock();
  102. return dp;
  103. }
  104. /* Must be called with rcu_read_lock or RTNL lock. */
  105. const char *ovs_dp_name(const struct datapath *dp)
  106. {
  107. struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL);
  108. return vport->ops->get_name(vport);
  109. }
  110. static int get_dpifindex(struct datapath *dp)
  111. {
  112. struct vport *local;
  113. int ifindex;
  114. rcu_read_lock();
  115. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  116. if (local)
  117. ifindex = local->ops->get_ifindex(local);
  118. else
  119. ifindex = 0;
  120. rcu_read_unlock();
  121. return ifindex;
  122. }
  123. static void destroy_dp_rcu(struct rcu_head *rcu)
  124. {
  125. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  126. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
  127. free_percpu(dp->stats_percpu);
  128. release_net(ovs_dp_get_net(dp));
  129. kfree(dp->ports);
  130. kfree(dp);
  131. }
  132. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  133. u16 port_no)
  134. {
  135. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  136. }
  137. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  138. {
  139. struct vport *vport;
  140. struct hlist_node *n;
  141. struct hlist_head *head;
  142. head = vport_hash_bucket(dp, port_no);
  143. hlist_for_each_entry_rcu(vport, n, head, dp_hash_node) {
  144. if (vport->port_no == port_no)
  145. return vport;
  146. }
  147. return NULL;
  148. }
  149. /* Called with RTNL lock and genl_lock. */
  150. static struct vport *new_vport(const struct vport_parms *parms)
  151. {
  152. struct vport *vport;
  153. vport = ovs_vport_add(parms);
  154. if (!IS_ERR(vport)) {
  155. struct datapath *dp = parms->dp;
  156. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  157. hlist_add_head_rcu(&vport->dp_hash_node, head);
  158. }
  159. return vport;
  160. }
  161. /* Called with RTNL lock. */
  162. void ovs_dp_detach_port(struct vport *p)
  163. {
  164. ASSERT_RTNL();
  165. /* First drop references to device. */
  166. hlist_del_rcu(&p->dp_hash_node);
  167. /* Then destroy it. */
  168. ovs_vport_del(p);
  169. }
  170. /* Must be called with rcu_read_lock. */
  171. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  172. {
  173. struct datapath *dp = p->dp;
  174. struct sw_flow *flow;
  175. struct dp_stats_percpu *stats;
  176. struct sw_flow_key key;
  177. u64 *stats_counter;
  178. int error;
  179. int key_len;
  180. stats = this_cpu_ptr(dp->stats_percpu);
  181. /* Extract flow from 'skb' into 'key'. */
  182. error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
  183. if (unlikely(error)) {
  184. kfree_skb(skb);
  185. return;
  186. }
  187. /* Look up flow. */
  188. flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
  189. if (unlikely(!flow)) {
  190. struct dp_upcall_info upcall;
  191. upcall.cmd = OVS_PACKET_CMD_MISS;
  192. upcall.key = &key;
  193. upcall.userdata = NULL;
  194. upcall.portid = p->upcall_portid;
  195. ovs_dp_upcall(dp, skb, &upcall);
  196. consume_skb(skb);
  197. stats_counter = &stats->n_missed;
  198. goto out;
  199. }
  200. OVS_CB(skb)->flow = flow;
  201. stats_counter = &stats->n_hit;
  202. ovs_flow_used(OVS_CB(skb)->flow, skb);
  203. ovs_execute_actions(dp, skb);
  204. out:
  205. /* Update datapath statistics. */
  206. u64_stats_update_begin(&stats->sync);
  207. (*stats_counter)++;
  208. u64_stats_update_end(&stats->sync);
  209. }
  210. static struct genl_family dp_packet_genl_family = {
  211. .id = GENL_ID_GENERATE,
  212. .hdrsize = sizeof(struct ovs_header),
  213. .name = OVS_PACKET_FAMILY,
  214. .version = OVS_PACKET_VERSION,
  215. .maxattr = OVS_PACKET_ATTR_MAX,
  216. .netnsok = true
  217. };
  218. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  219. const struct dp_upcall_info *upcall_info)
  220. {
  221. struct dp_stats_percpu *stats;
  222. int dp_ifindex;
  223. int err;
  224. if (upcall_info->portid == 0) {
  225. err = -ENOTCONN;
  226. goto err;
  227. }
  228. dp_ifindex = get_dpifindex(dp);
  229. if (!dp_ifindex) {
  230. err = -ENODEV;
  231. goto err;
  232. }
  233. if (!skb_is_gso(skb))
  234. err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  235. else
  236. err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  237. if (err)
  238. goto err;
  239. return 0;
  240. err:
  241. stats = this_cpu_ptr(dp->stats_percpu);
  242. u64_stats_update_begin(&stats->sync);
  243. stats->n_lost++;
  244. u64_stats_update_end(&stats->sync);
  245. return err;
  246. }
  247. static int queue_gso_packets(struct net *net, int dp_ifindex,
  248. struct sk_buff *skb,
  249. const struct dp_upcall_info *upcall_info)
  250. {
  251. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  252. struct dp_upcall_info later_info;
  253. struct sw_flow_key later_key;
  254. struct sk_buff *segs, *nskb;
  255. int err;
  256. segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
  257. if (IS_ERR(segs))
  258. return PTR_ERR(segs);
  259. /* Queue all of the segments. */
  260. skb = segs;
  261. do {
  262. err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
  263. if (err)
  264. break;
  265. if (skb == segs && gso_type & SKB_GSO_UDP) {
  266. /* The initial flow key extracted by ovs_flow_extract()
  267. * in this case is for a first fragment, so we need to
  268. * properly mark later fragments.
  269. */
  270. later_key = *upcall_info->key;
  271. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  272. later_info = *upcall_info;
  273. later_info.key = &later_key;
  274. upcall_info = &later_info;
  275. }
  276. } while ((skb = skb->next));
  277. /* Free all of the segments. */
  278. skb = segs;
  279. do {
  280. nskb = skb->next;
  281. if (err)
  282. kfree_skb(skb);
  283. else
  284. consume_skb(skb);
  285. } while ((skb = nskb));
  286. return err;
  287. }
  288. static int queue_userspace_packet(struct net *net, int dp_ifindex,
  289. struct sk_buff *skb,
  290. const struct dp_upcall_info *upcall_info)
  291. {
  292. struct ovs_header *upcall;
  293. struct sk_buff *nskb = NULL;
  294. struct sk_buff *user_skb; /* to be queued to userspace */
  295. struct nlattr *nla;
  296. unsigned int len;
  297. int err;
  298. if (vlan_tx_tag_present(skb)) {
  299. nskb = skb_clone(skb, GFP_ATOMIC);
  300. if (!nskb)
  301. return -ENOMEM;
  302. nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
  303. if (!nskb)
  304. return -ENOMEM;
  305. nskb->vlan_tci = 0;
  306. skb = nskb;
  307. }
  308. if (nla_attr_size(skb->len) > USHRT_MAX) {
  309. err = -EFBIG;
  310. goto out;
  311. }
  312. len = sizeof(struct ovs_header);
  313. len += nla_total_size(skb->len);
  314. len += nla_total_size(FLOW_BUFSIZE);
  315. if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
  316. len += nla_total_size(8);
  317. user_skb = genlmsg_new(len, GFP_ATOMIC);
  318. if (!user_skb) {
  319. err = -ENOMEM;
  320. goto out;
  321. }
  322. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  323. 0, upcall_info->cmd);
  324. upcall->dp_ifindex = dp_ifindex;
  325. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  326. ovs_flow_to_nlattrs(upcall_info->key, user_skb);
  327. nla_nest_end(user_skb, nla);
  328. if (upcall_info->userdata)
  329. nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
  330. nla_get_u64(upcall_info->userdata));
  331. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  332. skb_copy_and_csum_dev(skb, nla_data(nla));
  333. err = genlmsg_unicast(net, user_skb, upcall_info->portid);
  334. out:
  335. kfree_skb(nskb);
  336. return err;
  337. }
  338. /* Called with genl_mutex. */
  339. static int flush_flows(struct datapath *dp)
  340. {
  341. struct flow_table *old_table;
  342. struct flow_table *new_table;
  343. old_table = genl_dereference(dp->table);
  344. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  345. if (!new_table)
  346. return -ENOMEM;
  347. rcu_assign_pointer(dp->table, new_table);
  348. ovs_flow_tbl_deferred_destroy(old_table);
  349. return 0;
  350. }
  351. static int validate_actions(const struct nlattr *attr,
  352. const struct sw_flow_key *key, int depth);
  353. static int validate_sample(const struct nlattr *attr,
  354. const struct sw_flow_key *key, int depth)
  355. {
  356. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  357. const struct nlattr *probability, *actions;
  358. const struct nlattr *a;
  359. int rem;
  360. memset(attrs, 0, sizeof(attrs));
  361. nla_for_each_nested(a, attr, rem) {
  362. int type = nla_type(a);
  363. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  364. return -EINVAL;
  365. attrs[type] = a;
  366. }
  367. if (rem)
  368. return -EINVAL;
  369. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  370. if (!probability || nla_len(probability) != sizeof(u32))
  371. return -EINVAL;
  372. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  373. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  374. return -EINVAL;
  375. return validate_actions(actions, key, depth + 1);
  376. }
  377. static int validate_tp_port(const struct sw_flow_key *flow_key)
  378. {
  379. if (flow_key->eth.type == htons(ETH_P_IP)) {
  380. if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
  381. return 0;
  382. } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
  383. if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
  384. return 0;
  385. }
  386. return -EINVAL;
  387. }
  388. static int validate_set(const struct nlattr *a,
  389. const struct sw_flow_key *flow_key)
  390. {
  391. const struct nlattr *ovs_key = nla_data(a);
  392. int key_type = nla_type(ovs_key);
  393. /* There can be only one key in a action */
  394. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  395. return -EINVAL;
  396. if (key_type > OVS_KEY_ATTR_MAX ||
  397. nla_len(ovs_key) != ovs_key_lens[key_type])
  398. return -EINVAL;
  399. switch (key_type) {
  400. const struct ovs_key_ipv4 *ipv4_key;
  401. const struct ovs_key_ipv6 *ipv6_key;
  402. case OVS_KEY_ATTR_PRIORITY:
  403. case OVS_KEY_ATTR_SKB_MARK:
  404. case OVS_KEY_ATTR_ETHERNET:
  405. break;
  406. case OVS_KEY_ATTR_IPV4:
  407. if (flow_key->eth.type != htons(ETH_P_IP))
  408. return -EINVAL;
  409. if (!flow_key->ip.proto)
  410. return -EINVAL;
  411. ipv4_key = nla_data(ovs_key);
  412. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  413. return -EINVAL;
  414. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  415. return -EINVAL;
  416. break;
  417. case OVS_KEY_ATTR_IPV6:
  418. if (flow_key->eth.type != htons(ETH_P_IPV6))
  419. return -EINVAL;
  420. if (!flow_key->ip.proto)
  421. return -EINVAL;
  422. ipv6_key = nla_data(ovs_key);
  423. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  424. return -EINVAL;
  425. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  426. return -EINVAL;
  427. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  428. return -EINVAL;
  429. break;
  430. case OVS_KEY_ATTR_TCP:
  431. if (flow_key->ip.proto != IPPROTO_TCP)
  432. return -EINVAL;
  433. return validate_tp_port(flow_key);
  434. case OVS_KEY_ATTR_UDP:
  435. if (flow_key->ip.proto != IPPROTO_UDP)
  436. return -EINVAL;
  437. return validate_tp_port(flow_key);
  438. default:
  439. return -EINVAL;
  440. }
  441. return 0;
  442. }
  443. static int validate_userspace(const struct nlattr *attr)
  444. {
  445. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  446. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  447. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
  448. };
  449. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  450. int error;
  451. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  452. attr, userspace_policy);
  453. if (error)
  454. return error;
  455. if (!a[OVS_USERSPACE_ATTR_PID] ||
  456. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  457. return -EINVAL;
  458. return 0;
  459. }
  460. static int validate_actions(const struct nlattr *attr,
  461. const struct sw_flow_key *key, int depth)
  462. {
  463. const struct nlattr *a;
  464. int rem, err;
  465. if (depth >= SAMPLE_ACTION_DEPTH)
  466. return -EOVERFLOW;
  467. nla_for_each_nested(a, attr, rem) {
  468. /* Expected argument lengths, (u32)-1 for variable length. */
  469. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  470. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  471. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  472. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  473. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  474. [OVS_ACTION_ATTR_SET] = (u32)-1,
  475. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  476. };
  477. const struct ovs_action_push_vlan *vlan;
  478. int type = nla_type(a);
  479. if (type > OVS_ACTION_ATTR_MAX ||
  480. (action_lens[type] != nla_len(a) &&
  481. action_lens[type] != (u32)-1))
  482. return -EINVAL;
  483. switch (type) {
  484. case OVS_ACTION_ATTR_UNSPEC:
  485. return -EINVAL;
  486. case OVS_ACTION_ATTR_USERSPACE:
  487. err = validate_userspace(a);
  488. if (err)
  489. return err;
  490. break;
  491. case OVS_ACTION_ATTR_OUTPUT:
  492. if (nla_get_u32(a) >= DP_MAX_PORTS)
  493. return -EINVAL;
  494. break;
  495. case OVS_ACTION_ATTR_POP_VLAN:
  496. break;
  497. case OVS_ACTION_ATTR_PUSH_VLAN:
  498. vlan = nla_data(a);
  499. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  500. return -EINVAL;
  501. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  502. return -EINVAL;
  503. break;
  504. case OVS_ACTION_ATTR_SET:
  505. err = validate_set(a, key);
  506. if (err)
  507. return err;
  508. break;
  509. case OVS_ACTION_ATTR_SAMPLE:
  510. err = validate_sample(a, key, depth);
  511. if (err)
  512. return err;
  513. break;
  514. default:
  515. return -EINVAL;
  516. }
  517. }
  518. if (rem > 0)
  519. return -EINVAL;
  520. return 0;
  521. }
  522. static void clear_stats(struct sw_flow *flow)
  523. {
  524. flow->used = 0;
  525. flow->tcp_flags = 0;
  526. flow->packet_count = 0;
  527. flow->byte_count = 0;
  528. }
  529. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  530. {
  531. struct ovs_header *ovs_header = info->userhdr;
  532. struct nlattr **a = info->attrs;
  533. struct sw_flow_actions *acts;
  534. struct sk_buff *packet;
  535. struct sw_flow *flow;
  536. struct datapath *dp;
  537. struct ethhdr *eth;
  538. int len;
  539. int err;
  540. int key_len;
  541. err = -EINVAL;
  542. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  543. !a[OVS_PACKET_ATTR_ACTIONS] ||
  544. nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
  545. goto err;
  546. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  547. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  548. err = -ENOMEM;
  549. if (!packet)
  550. goto err;
  551. skb_reserve(packet, NET_IP_ALIGN);
  552. memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
  553. skb_reset_mac_header(packet);
  554. eth = eth_hdr(packet);
  555. /* Normally, setting the skb 'protocol' field would be handled by a
  556. * call to eth_type_trans(), but it assumes there's a sending
  557. * device, which we may not have. */
  558. if (ntohs(eth->h_proto) >= 1536)
  559. packet->protocol = eth->h_proto;
  560. else
  561. packet->protocol = htons(ETH_P_802_2);
  562. /* Build an sw_flow for sending this packet. */
  563. flow = ovs_flow_alloc();
  564. err = PTR_ERR(flow);
  565. if (IS_ERR(flow))
  566. goto err_kfree_skb;
  567. err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
  568. if (err)
  569. goto err_flow_free;
  570. err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
  571. &flow->key.phy.skb_mark,
  572. &flow->key.phy.in_port,
  573. a[OVS_PACKET_ATTR_KEY]);
  574. if (err)
  575. goto err_flow_free;
  576. err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
  577. if (err)
  578. goto err_flow_free;
  579. flow->hash = ovs_flow_hash(&flow->key, key_len);
  580. acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
  581. err = PTR_ERR(acts);
  582. if (IS_ERR(acts))
  583. goto err_flow_free;
  584. rcu_assign_pointer(flow->sf_acts, acts);
  585. OVS_CB(packet)->flow = flow;
  586. packet->priority = flow->key.phy.priority;
  587. packet->mark = flow->key.phy.skb_mark;
  588. rcu_read_lock();
  589. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  590. err = -ENODEV;
  591. if (!dp)
  592. goto err_unlock;
  593. local_bh_disable();
  594. err = ovs_execute_actions(dp, packet);
  595. local_bh_enable();
  596. rcu_read_unlock();
  597. ovs_flow_free(flow);
  598. return err;
  599. err_unlock:
  600. rcu_read_unlock();
  601. err_flow_free:
  602. ovs_flow_free(flow);
  603. err_kfree_skb:
  604. kfree_skb(packet);
  605. err:
  606. return err;
  607. }
  608. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  609. [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
  610. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  611. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  612. };
  613. static struct genl_ops dp_packet_genl_ops[] = {
  614. { .cmd = OVS_PACKET_CMD_EXECUTE,
  615. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  616. .policy = packet_policy,
  617. .doit = ovs_packet_cmd_execute
  618. }
  619. };
  620. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  621. {
  622. int i;
  623. struct flow_table *table = genl_dereference(dp->table);
  624. stats->n_flows = ovs_flow_tbl_count(table);
  625. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  626. for_each_possible_cpu(i) {
  627. const struct dp_stats_percpu *percpu_stats;
  628. struct dp_stats_percpu local_stats;
  629. unsigned int start;
  630. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  631. do {
  632. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  633. local_stats = *percpu_stats;
  634. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  635. stats->n_hit += local_stats.n_hit;
  636. stats->n_missed += local_stats.n_missed;
  637. stats->n_lost += local_stats.n_lost;
  638. }
  639. }
  640. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  641. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  642. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  643. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  644. };
  645. static struct genl_family dp_flow_genl_family = {
  646. .id = GENL_ID_GENERATE,
  647. .hdrsize = sizeof(struct ovs_header),
  648. .name = OVS_FLOW_FAMILY,
  649. .version = OVS_FLOW_VERSION,
  650. .maxattr = OVS_FLOW_ATTR_MAX,
  651. .netnsok = true
  652. };
  653. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  654. .name = OVS_FLOW_MCGROUP
  655. };
  656. /* Called with genl_lock. */
  657. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  658. struct sk_buff *skb, u32 portid,
  659. u32 seq, u32 flags, u8 cmd)
  660. {
  661. const int skb_orig_len = skb->len;
  662. const struct sw_flow_actions *sf_acts;
  663. struct ovs_flow_stats stats;
  664. struct ovs_header *ovs_header;
  665. struct nlattr *nla;
  666. unsigned long used;
  667. u8 tcp_flags;
  668. int err;
  669. sf_acts = rcu_dereference_protected(flow->sf_acts,
  670. lockdep_genl_is_held());
  671. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  672. if (!ovs_header)
  673. return -EMSGSIZE;
  674. ovs_header->dp_ifindex = get_dpifindex(dp);
  675. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  676. if (!nla)
  677. goto nla_put_failure;
  678. err = ovs_flow_to_nlattrs(&flow->key, skb);
  679. if (err)
  680. goto error;
  681. nla_nest_end(skb, nla);
  682. spin_lock_bh(&flow->lock);
  683. used = flow->used;
  684. stats.n_packets = flow->packet_count;
  685. stats.n_bytes = flow->byte_count;
  686. tcp_flags = flow->tcp_flags;
  687. spin_unlock_bh(&flow->lock);
  688. if (used &&
  689. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  690. goto nla_put_failure;
  691. if (stats.n_packets &&
  692. nla_put(skb, OVS_FLOW_ATTR_STATS,
  693. sizeof(struct ovs_flow_stats), &stats))
  694. goto nla_put_failure;
  695. if (tcp_flags &&
  696. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  697. goto nla_put_failure;
  698. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  699. * this is the first flow to be dumped into 'skb'. This is unusual for
  700. * Netlink but individual action lists can be longer than
  701. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  702. * The userspace caller can always fetch the actions separately if it
  703. * really wants them. (Most userspace callers in fact don't care.)
  704. *
  705. * This can only fail for dump operations because the skb is always
  706. * properly sized for single flows.
  707. */
  708. err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
  709. sf_acts->actions);
  710. if (err < 0 && skb_orig_len)
  711. goto error;
  712. return genlmsg_end(skb, ovs_header);
  713. nla_put_failure:
  714. err = -EMSGSIZE;
  715. error:
  716. genlmsg_cancel(skb, ovs_header);
  717. return err;
  718. }
  719. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  720. {
  721. const struct sw_flow_actions *sf_acts;
  722. int len;
  723. sf_acts = rcu_dereference_protected(flow->sf_acts,
  724. lockdep_genl_is_held());
  725. /* OVS_FLOW_ATTR_KEY */
  726. len = nla_total_size(FLOW_BUFSIZE);
  727. /* OVS_FLOW_ATTR_ACTIONS */
  728. len += nla_total_size(sf_acts->actions_len);
  729. /* OVS_FLOW_ATTR_STATS */
  730. len += nla_total_size(sizeof(struct ovs_flow_stats));
  731. /* OVS_FLOW_ATTR_TCP_FLAGS */
  732. len += nla_total_size(1);
  733. /* OVS_FLOW_ATTR_USED */
  734. len += nla_total_size(8);
  735. len += NLMSG_ALIGN(sizeof(struct ovs_header));
  736. return genlmsg_new(len, GFP_KERNEL);
  737. }
  738. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  739. struct datapath *dp,
  740. u32 portid, u32 seq, u8 cmd)
  741. {
  742. struct sk_buff *skb;
  743. int retval;
  744. skb = ovs_flow_cmd_alloc_info(flow);
  745. if (!skb)
  746. return ERR_PTR(-ENOMEM);
  747. retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
  748. BUG_ON(retval < 0);
  749. return skb;
  750. }
  751. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  752. {
  753. struct nlattr **a = info->attrs;
  754. struct ovs_header *ovs_header = info->userhdr;
  755. struct sw_flow_key key;
  756. struct sw_flow *flow;
  757. struct sk_buff *reply;
  758. struct datapath *dp;
  759. struct flow_table *table;
  760. int error;
  761. int key_len;
  762. /* Extract key. */
  763. error = -EINVAL;
  764. if (!a[OVS_FLOW_ATTR_KEY])
  765. goto error;
  766. error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  767. if (error)
  768. goto error;
  769. /* Validate actions. */
  770. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  771. error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
  772. if (error)
  773. goto error;
  774. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  775. error = -EINVAL;
  776. goto error;
  777. }
  778. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  779. error = -ENODEV;
  780. if (!dp)
  781. goto error;
  782. table = genl_dereference(dp->table);
  783. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  784. if (!flow) {
  785. struct sw_flow_actions *acts;
  786. /* Bail out if we're not allowed to create a new flow. */
  787. error = -ENOENT;
  788. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  789. goto error;
  790. /* Expand table, if necessary, to make room. */
  791. if (ovs_flow_tbl_need_to_expand(table)) {
  792. struct flow_table *new_table;
  793. new_table = ovs_flow_tbl_expand(table);
  794. if (!IS_ERR(new_table)) {
  795. rcu_assign_pointer(dp->table, new_table);
  796. ovs_flow_tbl_deferred_destroy(table);
  797. table = genl_dereference(dp->table);
  798. }
  799. }
  800. /* Allocate flow. */
  801. flow = ovs_flow_alloc();
  802. if (IS_ERR(flow)) {
  803. error = PTR_ERR(flow);
  804. goto error;
  805. }
  806. flow->key = key;
  807. clear_stats(flow);
  808. /* Obtain actions. */
  809. acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
  810. error = PTR_ERR(acts);
  811. if (IS_ERR(acts))
  812. goto error_free_flow;
  813. rcu_assign_pointer(flow->sf_acts, acts);
  814. /* Put flow in bucket. */
  815. flow->hash = ovs_flow_hash(&key, key_len);
  816. ovs_flow_tbl_insert(table, flow);
  817. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  818. info->snd_seq,
  819. OVS_FLOW_CMD_NEW);
  820. } else {
  821. /* We found a matching flow. */
  822. struct sw_flow_actions *old_acts;
  823. struct nlattr *acts_attrs;
  824. /* Bail out if we're not allowed to modify an existing flow.
  825. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  826. * because Generic Netlink treats the latter as a dump
  827. * request. We also accept NLM_F_EXCL in case that bug ever
  828. * gets fixed.
  829. */
  830. error = -EEXIST;
  831. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  832. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  833. goto error;
  834. /* Update actions. */
  835. old_acts = rcu_dereference_protected(flow->sf_acts,
  836. lockdep_genl_is_held());
  837. acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
  838. if (acts_attrs &&
  839. (old_acts->actions_len != nla_len(acts_attrs) ||
  840. memcmp(old_acts->actions, nla_data(acts_attrs),
  841. old_acts->actions_len))) {
  842. struct sw_flow_actions *new_acts;
  843. new_acts = ovs_flow_actions_alloc(acts_attrs);
  844. error = PTR_ERR(new_acts);
  845. if (IS_ERR(new_acts))
  846. goto error;
  847. rcu_assign_pointer(flow->sf_acts, new_acts);
  848. ovs_flow_deferred_free_acts(old_acts);
  849. }
  850. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  851. info->snd_seq, OVS_FLOW_CMD_NEW);
  852. /* Clear stats. */
  853. if (a[OVS_FLOW_ATTR_CLEAR]) {
  854. spin_lock_bh(&flow->lock);
  855. clear_stats(flow);
  856. spin_unlock_bh(&flow->lock);
  857. }
  858. }
  859. if (!IS_ERR(reply))
  860. genl_notify(reply, genl_info_net(info), info->snd_portid,
  861. ovs_dp_flow_multicast_group.id, info->nlhdr,
  862. GFP_KERNEL);
  863. else
  864. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  865. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  866. return 0;
  867. error_free_flow:
  868. ovs_flow_free(flow);
  869. error:
  870. return error;
  871. }
  872. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  873. {
  874. struct nlattr **a = info->attrs;
  875. struct ovs_header *ovs_header = info->userhdr;
  876. struct sw_flow_key key;
  877. struct sk_buff *reply;
  878. struct sw_flow *flow;
  879. struct datapath *dp;
  880. struct flow_table *table;
  881. int err;
  882. int key_len;
  883. if (!a[OVS_FLOW_ATTR_KEY])
  884. return -EINVAL;
  885. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  886. if (err)
  887. return err;
  888. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  889. if (!dp)
  890. return -ENODEV;
  891. table = genl_dereference(dp->table);
  892. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  893. if (!flow)
  894. return -ENOENT;
  895. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  896. info->snd_seq, OVS_FLOW_CMD_NEW);
  897. if (IS_ERR(reply))
  898. return PTR_ERR(reply);
  899. return genlmsg_reply(reply, info);
  900. }
  901. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  902. {
  903. struct nlattr **a = info->attrs;
  904. struct ovs_header *ovs_header = info->userhdr;
  905. struct sw_flow_key key;
  906. struct sk_buff *reply;
  907. struct sw_flow *flow;
  908. struct datapath *dp;
  909. struct flow_table *table;
  910. int err;
  911. int key_len;
  912. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  913. if (!dp)
  914. return -ENODEV;
  915. if (!a[OVS_FLOW_ATTR_KEY])
  916. return flush_flows(dp);
  917. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  918. if (err)
  919. return err;
  920. table = genl_dereference(dp->table);
  921. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  922. if (!flow)
  923. return -ENOENT;
  924. reply = ovs_flow_cmd_alloc_info(flow);
  925. if (!reply)
  926. return -ENOMEM;
  927. ovs_flow_tbl_remove(table, flow);
  928. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
  929. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  930. BUG_ON(err < 0);
  931. ovs_flow_deferred_free(flow);
  932. genl_notify(reply, genl_info_net(info), info->snd_portid,
  933. ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
  934. return 0;
  935. }
  936. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  937. {
  938. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  939. struct datapath *dp;
  940. struct flow_table *table;
  941. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  942. if (!dp)
  943. return -ENODEV;
  944. table = genl_dereference(dp->table);
  945. for (;;) {
  946. struct sw_flow *flow;
  947. u32 bucket, obj;
  948. bucket = cb->args[0];
  949. obj = cb->args[1];
  950. flow = ovs_flow_tbl_next(table, &bucket, &obj);
  951. if (!flow)
  952. break;
  953. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  954. NETLINK_CB(cb->skb).portid,
  955. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  956. OVS_FLOW_CMD_NEW) < 0)
  957. break;
  958. cb->args[0] = bucket;
  959. cb->args[1] = obj;
  960. }
  961. return skb->len;
  962. }
  963. static struct genl_ops dp_flow_genl_ops[] = {
  964. { .cmd = OVS_FLOW_CMD_NEW,
  965. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  966. .policy = flow_policy,
  967. .doit = ovs_flow_cmd_new_or_set
  968. },
  969. { .cmd = OVS_FLOW_CMD_DEL,
  970. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  971. .policy = flow_policy,
  972. .doit = ovs_flow_cmd_del
  973. },
  974. { .cmd = OVS_FLOW_CMD_GET,
  975. .flags = 0, /* OK for unprivileged users. */
  976. .policy = flow_policy,
  977. .doit = ovs_flow_cmd_get,
  978. .dumpit = ovs_flow_cmd_dump
  979. },
  980. { .cmd = OVS_FLOW_CMD_SET,
  981. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  982. .policy = flow_policy,
  983. .doit = ovs_flow_cmd_new_or_set,
  984. },
  985. };
  986. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  987. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  988. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  989. };
  990. static struct genl_family dp_datapath_genl_family = {
  991. .id = GENL_ID_GENERATE,
  992. .hdrsize = sizeof(struct ovs_header),
  993. .name = OVS_DATAPATH_FAMILY,
  994. .version = OVS_DATAPATH_VERSION,
  995. .maxattr = OVS_DP_ATTR_MAX,
  996. .netnsok = true
  997. };
  998. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  999. .name = OVS_DATAPATH_MCGROUP
  1000. };
  1001. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1002. u32 portid, u32 seq, u32 flags, u8 cmd)
  1003. {
  1004. struct ovs_header *ovs_header;
  1005. struct ovs_dp_stats dp_stats;
  1006. int err;
  1007. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1008. flags, cmd);
  1009. if (!ovs_header)
  1010. goto error;
  1011. ovs_header->dp_ifindex = get_dpifindex(dp);
  1012. rcu_read_lock();
  1013. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1014. rcu_read_unlock();
  1015. if (err)
  1016. goto nla_put_failure;
  1017. get_dp_stats(dp, &dp_stats);
  1018. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
  1019. goto nla_put_failure;
  1020. return genlmsg_end(skb, ovs_header);
  1021. nla_put_failure:
  1022. genlmsg_cancel(skb, ovs_header);
  1023. error:
  1024. return -EMSGSIZE;
  1025. }
  1026. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
  1027. u32 seq, u8 cmd)
  1028. {
  1029. struct sk_buff *skb;
  1030. int retval;
  1031. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1032. if (!skb)
  1033. return ERR_PTR(-ENOMEM);
  1034. retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
  1035. if (retval < 0) {
  1036. kfree_skb(skb);
  1037. return ERR_PTR(retval);
  1038. }
  1039. return skb;
  1040. }
  1041. /* Called with genl_mutex and optionally with RTNL lock also. */
  1042. static struct datapath *lookup_datapath(struct net *net,
  1043. struct ovs_header *ovs_header,
  1044. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1045. {
  1046. struct datapath *dp;
  1047. if (!a[OVS_DP_ATTR_NAME])
  1048. dp = get_dp(net, ovs_header->dp_ifindex);
  1049. else {
  1050. struct vport *vport;
  1051. rcu_read_lock();
  1052. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1053. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1054. rcu_read_unlock();
  1055. }
  1056. return dp ? dp : ERR_PTR(-ENODEV);
  1057. }
  1058. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1059. {
  1060. struct nlattr **a = info->attrs;
  1061. struct vport_parms parms;
  1062. struct sk_buff *reply;
  1063. struct datapath *dp;
  1064. struct vport *vport;
  1065. struct ovs_net *ovs_net;
  1066. int err, i;
  1067. err = -EINVAL;
  1068. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1069. goto err;
  1070. rtnl_lock();
  1071. err = -ENOMEM;
  1072. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1073. if (dp == NULL)
  1074. goto err_unlock_rtnl;
  1075. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1076. /* Allocate table. */
  1077. err = -ENOMEM;
  1078. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1079. if (!dp->table)
  1080. goto err_free_dp;
  1081. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1082. if (!dp->stats_percpu) {
  1083. err = -ENOMEM;
  1084. goto err_destroy_table;
  1085. }
  1086. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1087. GFP_KERNEL);
  1088. if (!dp->ports) {
  1089. err = -ENOMEM;
  1090. goto err_destroy_percpu;
  1091. }
  1092. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1093. INIT_HLIST_HEAD(&dp->ports[i]);
  1094. /* Set up our datapath device. */
  1095. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1096. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1097. parms.options = NULL;
  1098. parms.dp = dp;
  1099. parms.port_no = OVSP_LOCAL;
  1100. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1101. vport = new_vport(&parms);
  1102. if (IS_ERR(vport)) {
  1103. err = PTR_ERR(vport);
  1104. if (err == -EBUSY)
  1105. err = -EEXIST;
  1106. goto err_destroy_ports_array;
  1107. }
  1108. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1109. info->snd_seq, OVS_DP_CMD_NEW);
  1110. err = PTR_ERR(reply);
  1111. if (IS_ERR(reply))
  1112. goto err_destroy_local_port;
  1113. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1114. list_add_tail(&dp->list_node, &ovs_net->dps);
  1115. rtnl_unlock();
  1116. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1117. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1118. GFP_KERNEL);
  1119. return 0;
  1120. err_destroy_local_port:
  1121. ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
  1122. err_destroy_ports_array:
  1123. kfree(dp->ports);
  1124. err_destroy_percpu:
  1125. free_percpu(dp->stats_percpu);
  1126. err_destroy_table:
  1127. ovs_flow_tbl_destroy(genl_dereference(dp->table));
  1128. err_free_dp:
  1129. release_net(ovs_dp_get_net(dp));
  1130. kfree(dp);
  1131. err_unlock_rtnl:
  1132. rtnl_unlock();
  1133. err:
  1134. return err;
  1135. }
  1136. /* Called with genl_mutex. */
  1137. static void __dp_destroy(struct datapath *dp)
  1138. {
  1139. int i;
  1140. rtnl_lock();
  1141. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1142. struct vport *vport;
  1143. struct hlist_node *node, *n;
  1144. hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
  1145. if (vport->port_no != OVSP_LOCAL)
  1146. ovs_dp_detach_port(vport);
  1147. }
  1148. list_del(&dp->list_node);
  1149. ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
  1150. /* rtnl_unlock() will wait until all the references to devices that
  1151. * are pending unregistration have been dropped. We do it here to
  1152. * ensure that any internal devices (which contain DP pointers) are
  1153. * fully destroyed before freeing the datapath.
  1154. */
  1155. rtnl_unlock();
  1156. call_rcu(&dp->rcu, destroy_dp_rcu);
  1157. }
  1158. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1159. {
  1160. struct sk_buff *reply;
  1161. struct datapath *dp;
  1162. int err;
  1163. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1164. err = PTR_ERR(dp);
  1165. if (IS_ERR(dp))
  1166. return err;
  1167. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1168. info->snd_seq, OVS_DP_CMD_DEL);
  1169. err = PTR_ERR(reply);
  1170. if (IS_ERR(reply))
  1171. return err;
  1172. __dp_destroy(dp);
  1173. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1174. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1175. GFP_KERNEL);
  1176. return 0;
  1177. }
  1178. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1179. {
  1180. struct sk_buff *reply;
  1181. struct datapath *dp;
  1182. int err;
  1183. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1184. if (IS_ERR(dp))
  1185. return PTR_ERR(dp);
  1186. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1187. info->snd_seq, OVS_DP_CMD_NEW);
  1188. if (IS_ERR(reply)) {
  1189. err = PTR_ERR(reply);
  1190. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1191. ovs_dp_datapath_multicast_group.id, err);
  1192. return 0;
  1193. }
  1194. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1195. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1196. GFP_KERNEL);
  1197. return 0;
  1198. }
  1199. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1200. {
  1201. struct sk_buff *reply;
  1202. struct datapath *dp;
  1203. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1204. if (IS_ERR(dp))
  1205. return PTR_ERR(dp);
  1206. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1207. info->snd_seq, OVS_DP_CMD_NEW);
  1208. if (IS_ERR(reply))
  1209. return PTR_ERR(reply);
  1210. return genlmsg_reply(reply, info);
  1211. }
  1212. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1213. {
  1214. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1215. struct datapath *dp;
  1216. int skip = cb->args[0];
  1217. int i = 0;
  1218. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1219. if (i >= skip &&
  1220. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1221. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1222. OVS_DP_CMD_NEW) < 0)
  1223. break;
  1224. i++;
  1225. }
  1226. cb->args[0] = i;
  1227. return skb->len;
  1228. }
  1229. static struct genl_ops dp_datapath_genl_ops[] = {
  1230. { .cmd = OVS_DP_CMD_NEW,
  1231. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1232. .policy = datapath_policy,
  1233. .doit = ovs_dp_cmd_new
  1234. },
  1235. { .cmd = OVS_DP_CMD_DEL,
  1236. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1237. .policy = datapath_policy,
  1238. .doit = ovs_dp_cmd_del
  1239. },
  1240. { .cmd = OVS_DP_CMD_GET,
  1241. .flags = 0, /* OK for unprivileged users. */
  1242. .policy = datapath_policy,
  1243. .doit = ovs_dp_cmd_get,
  1244. .dumpit = ovs_dp_cmd_dump
  1245. },
  1246. { .cmd = OVS_DP_CMD_SET,
  1247. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1248. .policy = datapath_policy,
  1249. .doit = ovs_dp_cmd_set,
  1250. },
  1251. };
  1252. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1253. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1254. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1255. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1256. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1257. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1258. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1259. };
  1260. static struct genl_family dp_vport_genl_family = {
  1261. .id = GENL_ID_GENERATE,
  1262. .hdrsize = sizeof(struct ovs_header),
  1263. .name = OVS_VPORT_FAMILY,
  1264. .version = OVS_VPORT_VERSION,
  1265. .maxattr = OVS_VPORT_ATTR_MAX,
  1266. .netnsok = true
  1267. };
  1268. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1269. .name = OVS_VPORT_MCGROUP
  1270. };
  1271. /* Called with RTNL lock or RCU read lock. */
  1272. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1273. u32 portid, u32 seq, u32 flags, u8 cmd)
  1274. {
  1275. struct ovs_header *ovs_header;
  1276. struct ovs_vport_stats vport_stats;
  1277. int err;
  1278. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1279. flags, cmd);
  1280. if (!ovs_header)
  1281. return -EMSGSIZE;
  1282. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1283. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1284. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1285. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1286. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1287. goto nla_put_failure;
  1288. ovs_vport_get_stats(vport, &vport_stats);
  1289. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1290. &vport_stats))
  1291. goto nla_put_failure;
  1292. err = ovs_vport_get_options(vport, skb);
  1293. if (err == -EMSGSIZE)
  1294. goto error;
  1295. return genlmsg_end(skb, ovs_header);
  1296. nla_put_failure:
  1297. err = -EMSGSIZE;
  1298. error:
  1299. genlmsg_cancel(skb, ovs_header);
  1300. return err;
  1301. }
  1302. /* Called with RTNL lock or RCU read lock. */
  1303. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1304. u32 seq, u8 cmd)
  1305. {
  1306. struct sk_buff *skb;
  1307. int retval;
  1308. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1309. if (!skb)
  1310. return ERR_PTR(-ENOMEM);
  1311. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1312. if (retval < 0) {
  1313. kfree_skb(skb);
  1314. return ERR_PTR(retval);
  1315. }
  1316. return skb;
  1317. }
  1318. /* Called with RTNL lock or RCU read lock. */
  1319. static struct vport *lookup_vport(struct net *net,
  1320. struct ovs_header *ovs_header,
  1321. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1322. {
  1323. struct datapath *dp;
  1324. struct vport *vport;
  1325. if (a[OVS_VPORT_ATTR_NAME]) {
  1326. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1327. if (!vport)
  1328. return ERR_PTR(-ENODEV);
  1329. if (ovs_header->dp_ifindex &&
  1330. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1331. return ERR_PTR(-ENODEV);
  1332. return vport;
  1333. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1334. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1335. if (port_no >= DP_MAX_PORTS)
  1336. return ERR_PTR(-EFBIG);
  1337. dp = get_dp(net, ovs_header->dp_ifindex);
  1338. if (!dp)
  1339. return ERR_PTR(-ENODEV);
  1340. vport = ovs_vport_rtnl_rcu(dp, port_no);
  1341. if (!vport)
  1342. return ERR_PTR(-ENOENT);
  1343. return vport;
  1344. } else
  1345. return ERR_PTR(-EINVAL);
  1346. }
  1347. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1348. {
  1349. struct nlattr **a = info->attrs;
  1350. struct ovs_header *ovs_header = info->userhdr;
  1351. struct vport_parms parms;
  1352. struct sk_buff *reply;
  1353. struct vport *vport;
  1354. struct datapath *dp;
  1355. u32 port_no;
  1356. int err;
  1357. err = -EINVAL;
  1358. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1359. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1360. goto exit;
  1361. rtnl_lock();
  1362. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1363. err = -ENODEV;
  1364. if (!dp)
  1365. goto exit_unlock;
  1366. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1367. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1368. err = -EFBIG;
  1369. if (port_no >= DP_MAX_PORTS)
  1370. goto exit_unlock;
  1371. vport = ovs_vport_rtnl_rcu(dp, port_no);
  1372. err = -EBUSY;
  1373. if (vport)
  1374. goto exit_unlock;
  1375. } else {
  1376. for (port_no = 1; ; port_no++) {
  1377. if (port_no >= DP_MAX_PORTS) {
  1378. err = -EFBIG;
  1379. goto exit_unlock;
  1380. }
  1381. vport = ovs_vport_rtnl(dp, port_no);
  1382. if (!vport)
  1383. break;
  1384. }
  1385. }
  1386. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1387. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1388. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1389. parms.dp = dp;
  1390. parms.port_no = port_no;
  1391. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1392. vport = new_vport(&parms);
  1393. err = PTR_ERR(vport);
  1394. if (IS_ERR(vport))
  1395. goto exit_unlock;
  1396. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1397. OVS_VPORT_CMD_NEW);
  1398. if (IS_ERR(reply)) {
  1399. err = PTR_ERR(reply);
  1400. ovs_dp_detach_port(vport);
  1401. goto exit_unlock;
  1402. }
  1403. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1404. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1405. exit_unlock:
  1406. rtnl_unlock();
  1407. exit:
  1408. return err;
  1409. }
  1410. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1411. {
  1412. struct nlattr **a = info->attrs;
  1413. struct sk_buff *reply;
  1414. struct vport *vport;
  1415. int err;
  1416. rtnl_lock();
  1417. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1418. err = PTR_ERR(vport);
  1419. if (IS_ERR(vport))
  1420. goto exit_unlock;
  1421. err = 0;
  1422. if (a[OVS_VPORT_ATTR_TYPE] &&
  1423. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
  1424. err = -EINVAL;
  1425. if (!err && a[OVS_VPORT_ATTR_OPTIONS])
  1426. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1427. if (err)
  1428. goto exit_unlock;
  1429. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1430. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1431. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1432. OVS_VPORT_CMD_NEW);
  1433. if (IS_ERR(reply)) {
  1434. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1435. ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
  1436. goto exit_unlock;
  1437. }
  1438. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1439. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1440. exit_unlock:
  1441. rtnl_unlock();
  1442. return err;
  1443. }
  1444. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1445. {
  1446. struct nlattr **a = info->attrs;
  1447. struct sk_buff *reply;
  1448. struct vport *vport;
  1449. int err;
  1450. rtnl_lock();
  1451. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1452. err = PTR_ERR(vport);
  1453. if (IS_ERR(vport))
  1454. goto exit_unlock;
  1455. if (vport->port_no == OVSP_LOCAL) {
  1456. err = -EINVAL;
  1457. goto exit_unlock;
  1458. }
  1459. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1460. OVS_VPORT_CMD_DEL);
  1461. err = PTR_ERR(reply);
  1462. if (IS_ERR(reply))
  1463. goto exit_unlock;
  1464. ovs_dp_detach_port(vport);
  1465. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1466. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1467. exit_unlock:
  1468. rtnl_unlock();
  1469. return err;
  1470. }
  1471. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1472. {
  1473. struct nlattr **a = info->attrs;
  1474. struct ovs_header *ovs_header = info->userhdr;
  1475. struct sk_buff *reply;
  1476. struct vport *vport;
  1477. int err;
  1478. rcu_read_lock();
  1479. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1480. err = PTR_ERR(vport);
  1481. if (IS_ERR(vport))
  1482. goto exit_unlock;
  1483. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1484. OVS_VPORT_CMD_NEW);
  1485. err = PTR_ERR(reply);
  1486. if (IS_ERR(reply))
  1487. goto exit_unlock;
  1488. rcu_read_unlock();
  1489. return genlmsg_reply(reply, info);
  1490. exit_unlock:
  1491. rcu_read_unlock();
  1492. return err;
  1493. }
  1494. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1495. {
  1496. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1497. struct datapath *dp;
  1498. int bucket = cb->args[0], skip = cb->args[1];
  1499. int i, j = 0;
  1500. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1501. if (!dp)
  1502. return -ENODEV;
  1503. rcu_read_lock();
  1504. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1505. struct vport *vport;
  1506. struct hlist_node *n;
  1507. j = 0;
  1508. hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
  1509. if (j >= skip &&
  1510. ovs_vport_cmd_fill_info(vport, skb,
  1511. NETLINK_CB(cb->skb).portid,
  1512. cb->nlh->nlmsg_seq,
  1513. NLM_F_MULTI,
  1514. OVS_VPORT_CMD_NEW) < 0)
  1515. goto out;
  1516. j++;
  1517. }
  1518. skip = 0;
  1519. }
  1520. out:
  1521. rcu_read_unlock();
  1522. cb->args[0] = i;
  1523. cb->args[1] = j;
  1524. return skb->len;
  1525. }
  1526. static struct genl_ops dp_vport_genl_ops[] = {
  1527. { .cmd = OVS_VPORT_CMD_NEW,
  1528. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1529. .policy = vport_policy,
  1530. .doit = ovs_vport_cmd_new
  1531. },
  1532. { .cmd = OVS_VPORT_CMD_DEL,
  1533. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1534. .policy = vport_policy,
  1535. .doit = ovs_vport_cmd_del
  1536. },
  1537. { .cmd = OVS_VPORT_CMD_GET,
  1538. .flags = 0, /* OK for unprivileged users. */
  1539. .policy = vport_policy,
  1540. .doit = ovs_vport_cmd_get,
  1541. .dumpit = ovs_vport_cmd_dump
  1542. },
  1543. { .cmd = OVS_VPORT_CMD_SET,
  1544. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1545. .policy = vport_policy,
  1546. .doit = ovs_vport_cmd_set,
  1547. },
  1548. };
  1549. struct genl_family_and_ops {
  1550. struct genl_family *family;
  1551. struct genl_ops *ops;
  1552. int n_ops;
  1553. struct genl_multicast_group *group;
  1554. };
  1555. static const struct genl_family_and_ops dp_genl_families[] = {
  1556. { &dp_datapath_genl_family,
  1557. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1558. &ovs_dp_datapath_multicast_group },
  1559. { &dp_vport_genl_family,
  1560. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1561. &ovs_dp_vport_multicast_group },
  1562. { &dp_flow_genl_family,
  1563. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1564. &ovs_dp_flow_multicast_group },
  1565. { &dp_packet_genl_family,
  1566. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1567. NULL },
  1568. };
  1569. static void dp_unregister_genl(int n_families)
  1570. {
  1571. int i;
  1572. for (i = 0; i < n_families; i++)
  1573. genl_unregister_family(dp_genl_families[i].family);
  1574. }
  1575. static int dp_register_genl(void)
  1576. {
  1577. int n_registered;
  1578. int err;
  1579. int i;
  1580. n_registered = 0;
  1581. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1582. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1583. err = genl_register_family_with_ops(f->family, f->ops,
  1584. f->n_ops);
  1585. if (err)
  1586. goto error;
  1587. n_registered++;
  1588. if (f->group) {
  1589. err = genl_register_mc_group(f->family, f->group);
  1590. if (err)
  1591. goto error;
  1592. }
  1593. }
  1594. return 0;
  1595. error:
  1596. dp_unregister_genl(n_registered);
  1597. return err;
  1598. }
  1599. static void rehash_flow_table(struct work_struct *work)
  1600. {
  1601. struct datapath *dp;
  1602. struct net *net;
  1603. genl_lock();
  1604. rtnl_lock();
  1605. for_each_net(net) {
  1606. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1607. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1608. struct flow_table *old_table = genl_dereference(dp->table);
  1609. struct flow_table *new_table;
  1610. new_table = ovs_flow_tbl_rehash(old_table);
  1611. if (!IS_ERR(new_table)) {
  1612. rcu_assign_pointer(dp->table, new_table);
  1613. ovs_flow_tbl_deferred_destroy(old_table);
  1614. }
  1615. }
  1616. }
  1617. rtnl_unlock();
  1618. genl_unlock();
  1619. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1620. }
  1621. static int __net_init ovs_init_net(struct net *net)
  1622. {
  1623. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1624. INIT_LIST_HEAD(&ovs_net->dps);
  1625. return 0;
  1626. }
  1627. static void __net_exit ovs_exit_net(struct net *net)
  1628. {
  1629. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1630. struct datapath *dp, *dp_next;
  1631. genl_lock();
  1632. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1633. __dp_destroy(dp);
  1634. genl_unlock();
  1635. }
  1636. static struct pernet_operations ovs_net_ops = {
  1637. .init = ovs_init_net,
  1638. .exit = ovs_exit_net,
  1639. .id = &ovs_net_id,
  1640. .size = sizeof(struct ovs_net),
  1641. };
  1642. static int __init dp_init(void)
  1643. {
  1644. struct sk_buff *dummy_skb;
  1645. int err;
  1646. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
  1647. pr_info("Open vSwitch switching datapath\n");
  1648. err = ovs_flow_init();
  1649. if (err)
  1650. goto error;
  1651. err = ovs_vport_init();
  1652. if (err)
  1653. goto error_flow_exit;
  1654. err = register_pernet_device(&ovs_net_ops);
  1655. if (err)
  1656. goto error_vport_exit;
  1657. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1658. if (err)
  1659. goto error_netns_exit;
  1660. err = dp_register_genl();
  1661. if (err < 0)
  1662. goto error_unreg_notifier;
  1663. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1664. return 0;
  1665. error_unreg_notifier:
  1666. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1667. error_netns_exit:
  1668. unregister_pernet_device(&ovs_net_ops);
  1669. error_vport_exit:
  1670. ovs_vport_exit();
  1671. error_flow_exit:
  1672. ovs_flow_exit();
  1673. error:
  1674. return err;
  1675. }
  1676. static void dp_cleanup(void)
  1677. {
  1678. cancel_delayed_work_sync(&rehash_flow_wq);
  1679. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1680. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1681. unregister_pernet_device(&ovs_net_ops);
  1682. rcu_barrier();
  1683. ovs_vport_exit();
  1684. ovs_flow_exit();
  1685. }
  1686. module_init(dp_init);
  1687. module_exit(dp_cleanup);
  1688. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1689. MODULE_LICENSE("GPL");